doumo0206 2016-05-12 11:30
浏览 13

受保护的数组上的php isset返回false,但其中一个键上的isset返回true

I am trying to analyse a curious case where some code regarding isset() behaves differently on my local Windows machine (PHP 7.0.3) from our Unix test server (recently updated to PHP 7.0.6). But when trying to create a simpler reproducible code snippet on the Windows machine, it behaves like the Unix server.

The line in question is

if (isset($this->settings[$Model->alias]['Log']->_schema['model'])) {

$this->settings[$Model->alias]['Log'] is an object, _schemais a protected property, which is an array. The key modelexists in this array. On the Unix server, the if-condition returns false. On the windows server it returns true. On the Unix test server before the update, as well as on our productive web server, it also returns true.

I understand that isset(), performed on a protected property, should always return false. So it shouldn't have worked in the first place. Checking on the property itself indeed returns always false, both on Windows and on Unix:

$this->settings[$Model->alias]['Log']->_schema) // false

But checking for a key of this array property behaves differently. On Windows and the productive server:

$this->settings[$Model->alias]['Log']->_schema['model']) // true

On the test server:

$this->settings[$Model->alias]['Log']->_schema['model']) // false

I then tried to create a simpler reproducible code snippet:

<?php
class Log {
    protected $_schema = array(
        'model' => array(
            'name' => 'change',
        ),
    );
}

class Model {
    public $alias = 'Contact';
}

class LogableBehavior {
    public $settings = array();

    function __construct() {
        $Model = new Model();

        $this->settings[$Model->alias]['Log'] = new Log();

        echo isset($this->settings[$Model->alias]['Log']->_schema)?'true':'false';
        echo '<br/>';
        echo isset($this->settings[$Model->alias]['Log']->_schema['model'])?'true':'false';
        echo '<br/>';
    }
}

$obj = new LogableBehavior();

This outputs

false
false

on all machines. This is what I would expect. But within the bigger project code isset($this->settings[$Model->alias]['Log']->_schema['model']) returns true, on my local Windows machine and the productive server.

I read in other places that when using isset() while checking for an array key, but the variable in question is actually a string instead of an array, the results can be unexpected. But this should not apply here, because the protected property _schema is an array when I debug it.

Any ideas for an explanation?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line