PHP 7.2 is_array 不等于真

PHP 7.2 is_array not eval to true

全部,我有一个 class,在顶部定义了一些变量,如下所示:

var $conditionStyle = '';

以后我可以这样设计一件东西:

$this -> conditionStyle = 'someStyle';

或多个这样的事情:

$this->conditionStyle[$this->styleRowsCount] = 'someStyle';

接下来我会看看 conditionStyle 是否是一个数组,像这样:

if(is_array($this-> conditionStyle) {...}

在 php 7.0 和更早的版本中,这个评价很好。对于 7.2,我必须使用 settype() 否则无法正确评估。这是 7.2 的问题还是 7.2 纠正了以前版本中的缺陷?

PHP 7.1 改变了这段代码的行为:

$x = '';
$x[3] = 'foo';

在 < 7.1 中,$x 是:

array (
   3 => 'foo',
)

在 >= 7.1 中,它是:

string '   f'

See it online at 3v4l.org.

这个改动不好PHP 7.1 Release Notes中提到:

The empty index operator is not supported for strings anymore
Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.

进行此更改的 PR 也有您注意到的副作用,并且 someone else commented 在该页面的注释部分。

您应该在一开始就将您的变量初始化为一个数组,以便在所有版本中工作。