PHP7覆盖魔术方法__get不再工作

时间:2017-02-22 10:51:32

标签: getter-setter php-7 magic-methods

我在PHP 5.6中使用了以下方法,它始终工作正常

public function __get($name){

            if(!empty($this->_dynamicFields[$name])){
                if(!empty($this->_dynamicData[$name])){
                    return $this->_dynamicData[$name];
                }else{
                    return null;
                }
            }else{
                return parent::__get($name); // That's where the error happens when an array is called in $name
            }
        }

现在我们将服务器升级到PHP7,当脚本使用数组调用get-method时,出现错误

$object->$attributes[0]

classname.Array未定义

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是由于PHP7中的Changes to the handling of indirect variables, properties, and methods打破了向后兼容性(另请参阅Uniform Variable Syntax)。

具体来说,在PHP5中,您的调用解释如下:

$object->$attributes[0] === $object->{$attributes[0]}

但是,在PHP7中,您的调用解释如下:

$object->$attributes[0] === ($object->$attributes)[0]

如果您将代码修改为明确的$object->{$attributes[0]},您应该会看到它再次按预期工作。