【问题标题】:Notice (8): Indirect modification of overloaded property LayoutHelper::$View has no effect [APP/View/Helper/LayoutHelper.php, line 48]注意(8):间接修改重载属性LayoutHelper::$View无效[APP/View/Helper/LayoutHelper.php,第48行]
【发布时间】:2015-09-22 17:30:28
【问题描述】:

在将 CakePHP 代码从 1.3 迁移到 2.x 时,我收到了这条通知消息:-

注意(8):间接修改重载属性LayoutHelper::$View无效[APP/View/Helper/LayoutHelper.php, line 48]

本通知代码为:-

function __construct($options = array()) {
    $this->View =& ClassRegistry::getObject('view');
    $this->__loadHooks();

    return parent::__construct($options);
}

我应该怎么做才能解决这个问题?

【问题讨论】:

  • "我应该怎么做才能解决这个问题?" - 开始使用搜索功能?这已经被问了几千次了。只需搜索错误消息。您不太可能是第一个遇到它的人。
  • @burzum 没有帮助。

标签: cakephp cakephp-2.x


【解决方案1】:

了解通知

该消息意味着正在设置一个属性,该属性可以通过魔术访问器访问 - 它实际上不会做任何事情。相当于这样:

<?php

class Foo {
        function __get($prop) {
            return [];
        }
}

$foo = new Foo;
$foo->bar['zum'] = "x";
print_r($foo->bar); // []

在这个人为的示例中,属性“bar”不存在,因此调用了魔术 getter,返回一个空数组 - 代码试图附加/写入这个魔术 getter-returned-value 并发出相同的通知:

注意:间接修改重载属性 Foo::$bar 在 /tmp/overload-example.php 第 12 行没有影响

print_r调用所示,$foo-&gt;bar的值没有改变。

正常的解决方案是声明属性,以便使用魔法吸气剂,即:

class Foo {
    public $bar = []; // Now it can be modified.

但是在这种情况下,这不是最合适的做法。

助手从 1.x 更改为 2.x

所有助手的构造函数在 2.x 中更改为 in the migration guide。详细阅读迁移指南,尤其是在遇到问题时。

在 1.3 中,所有助手都扩展了 the helper class,它没有构造函数,也没有对视图的引用。在 2.x 中,所有助手都扩展同一个类,但 do have a constructor and do keep a reference to the view class。有 2 个步骤与此错误相关,可将帮助程序升级为 2.x 兼容

改变构造函数:

无需重复父构造函数为您执行的逻辑,因此只需调用__loadHooks(如有必要):

function __construct(View $View, $settings = array()) {
    $this->__loadHooks();
    return parent::__construct($View, $settings);
}

更改对$this-&gt;View的引用

所有助手have access to the view instance,只需在您的助手代码中查找并替换即可:

$this->_View 

有了这个:

$this->View 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多