1

While migrating CakePHP code from 1.3 to 2.x I am getting this notice message:-

Notice (8): Indirect modification of overloaded property LayoutHelper::$View has no effect [APP/View/Helper/LayoutHelper.php, line 48]

The code for this notice is:-

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

    return parent::__construct($options);
}

What should I do to fix this?

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
Ankit
  • 233
  • 1
  • 4
  • 19
  • "*What should I do to fix this?*" - Start using the search function? This has been asked a few thousand times. Just search for the error message. It is unlikely you're the first person to encounter it. – floriank Sep 22 '15 at 22:40
  • possible duplicate of [PHP - Indirect modification of overloaded property](http://stackoverflow.com/questions/10454779/php-indirect-modification-of-overloaded-property) – floriank Sep 22 '15 at 22:41
  • @burzum not helpful. – AD7six Sep 23 '15 at 07:56

1 Answers1

1

Understanding the notice

The message means that a property is being set which is accessed with a magic accessor - it's not going to actually do anything. It is equivalent to this:

<?php

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

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

In this contrived example the property "bar" does not exist, and so the magic getter is called returning an empty array - the code is attempting to append/write to this magic-getter-returned-value and emits the same notice:

Notice: Indirect modification of overloaded property Foo::$bar has no effect in /tmp/overload-example.php on line 12

As shown by the print_r call, the value of $foo->bar has not changed.

The normal solution is to declare the property so that the magic getter is not used i.e.:

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

However in this case that's not the most appropriate thing to do.

Helpers changed from 1.x to 2.x

The constructor for all helpers changed in 2.x which is in the migration guide. Read the migration guide in detail, especially if you encounter a problem.

In 1.3 all helpers extend the helper class, it doesn't have a constructor, and does not keep a reference to the view. In 2.x all helpers extend the same class but do have a constructor and do keep a reference to the view class. There are 2 steps related to this error to upgrade the helper to be 2.x compatible

Change the constructor:

There is no need to repeat logic the parent constructor is doing for you so just call __loadHooks (if necessary):

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

Change references to $this->View

All helpers have access to the view instance, just find and replace in your helper code replacing this:

$this->_View 

With this:

$this->View 
AD7six
  • 63,116
  • 12
  • 91
  • 123