php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40828 Overloading: "Indirect modification" with "$this->foo[] = $x"
Submitted: 2007-03-15 19:35 UTC Modified: 2007-03-20 17:33 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: phpbugs at thequod dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5CVS-2007-03-15 (CVS) OS: Ubuntu Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phpbugs at thequod dot de
New email:
PHP Version: OS:

 

 [2007-03-15 19:35 UTC] phpbugs at thequod dot de
Description:
------------
If a member of an object is not defined and "gets initialized" by 
PHP after/during the overloading process, a notice ("Indirect 
modification of overloaded property") gets triggered when PHP has to 
initialize it as an array type.

It makes no difference, if __get() returns by reference instead (a 
ref to a null value), as Tony stated in 
http://bugs.php.net/bug.php?id=40823 (the code in his comment does 
not work (anymore?)).


Background: we have a base class "Plugin" in our project and it uses 
__get() for some properties and returns null otherwise.
Now, if some user-created plugin does
$this->foo[] = 'bar'
it will create this notice - which is quite confusing (if the 
derived plugin does not "init" $foo with "var $foo").


Can't PHP internally do "$this->test = array()"?


(This may likely be a dupe of http://bugs.php.net/bug.php?id=39337 - 
but that one got quite confusing, so this one here may become 
a "reference bogus bug" for this issue at least.)

Reproduce code:
---------------
<?php

class A
{
        function __get($name)
        {
                echo "__get() called.\n";
        }

        function A()
        {
                $this->test[] = 'foo';
        }
}

$A = new A();

?>


Expected result:
----------------
__get() called.


Actual result:
--------------
__get() called.

Notice: Indirect modification of overloaded property A::$test has no 
effect in foo.php on line 12


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-15 19:44 UTC] tony2001@php.net
>as Tony stated in http://bugs.php.net/bug.php?id=40823 
>(the code in his comment does not work (anymore?)).
The code in my comment works perfectly fine.

>Can't PHP internally do "$this->test = array()"?
No, it can't do your job for you. You have to do it in the __get() method.

 [2007-03-15 19:45 UTC] tony2001@php.net
Please, if you have any other _questions_ - ask them on a support forum.
 [2007-03-16 14:57 UTC] phpbugs at thequod dot de
>>Can't PHP internally do "$this->test = array()"?
>No, it can't do your job for you. You have to do it in the __get()
method.

1. It also won't work with
        function &__get($name)
        {
                echo "__get() called.\n";
                $r = array();
                return $r;
        }
2. It would not make sense to init any unknown/unhandled property 
as "array()" anyway
3. Your comment works, if you remove the "(array)" typecast in 
&__get() - it will cause "Only variable references should be 
returned by reference" otherwise
4. I'm not looking for support, but state that "$this->foo[]" is not 
supported in overloading - in contrast what you would expect

Please note that this is different from bug 40823, where _any_ 
property (which goes through __get()) gets handled as Foo::array!

What I want to work is:
Only handle some members as overloaded properties (e.g. $foo and 
$bar), but let all others work as intended - and that 
means "$A->foobar[]=" should work, just like "$foobar[]=" does.

Please make sure you really understand what the problem IMHO is 
here!

At least this should get closed as "Wont fix" and documented on 
http://php.net/manual/en/language.oop5.overloading.php.

See also 
http://php.net/manual/en/language.oop5.overloading.php#73512
 [2007-03-20 17:03 UTC] phpbugs at thequod dot de
Re-opening.
 [2007-03-20 17:33 UTC] tony2001@php.net
I've already explained everything, no need to repeat myself.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 15:01:27 2025 UTC