Month: 十二月 2018

启用mysqli报告,捕获PHP程序中未使用索引的查询语句.

启用mysqli报告,捕获PHP程序中未使用索引的查询语句.

使用mysqli_report($flags)函数

在代码开发和测试期间有助于改进查询的函数。根据标志,它报告来自mysqli函数调用或不使用索引的查询(或使用错误索引)的错误。

代码如下

<?php
/* 激活报告 */
mysqli_report(MYSQLI_REPORT_ALL);

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* 检查连接 */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* 此查询会报告错误 */
$result = mysqli_query("SELECT Name FROM Nonexistingtable WHERE population 
        

composer :错误:Could not find a version of package matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability

composer 安装 composer require --dev sandrokeil/php-to-zephir 时出现

Could not find a version of package matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability

解决方法:https://github.com/composer/composer/issues/5118

用以下命令解决:

composer require "sandrokeil/php-to-zephir @dev"

PHP 在不调用构造函数的情况下创建对象

PHP 在不调用构造函数的情况下创建对象

可以利用PHP的反射功能ReflectionClass :: newInstanceWithoutConstructor,实现这一需求。

例子:

<?php
class a
{
    public $foo=0;
    public $bar=9;
    public function __construct()
    {
        $this->foo=1;
        echo "调用构造函数\n";
    }
}
  
  $ref = new ReflectionClass('a');
  $inst = $ref->newInstanceWithoutConstructor();
  print_r($inst);

 

以上输出:

a Object
(
    [foo] => 0
    [bar]