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, _schema
is a protected property, which is an array. The key model
exists 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?