php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33998 Calling a getter within a getter does not work.
Submitted: 2005-08-04 22:22 UTC Modified: 2015-12-17 00:17 UTC
From: dan at stratitec dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.0.3 OS: Linux (Red Hat 9)
Private report: No CVE-ID: None
 [2005-08-04 22:22 UTC] dan at stratitec dot com
Description:
------------
Calling a variable using the __get() code from within a 
function that has been called, or is inside the __get() 
function itself results in the variable or result not 
being found. 
 
If this is to keep recursion from happening, I feel it's a 
poor choice. A programmer can ALWAYS shoot himself in the 
foot with infinite recursion, but placing limits like this 
is counterintuitive and prevents solutions. 
 
Also, since replacing $this->a with $this->__get('a') in 
the example allows the script to run as intended, a user 
could still use the __get function to recurse infinitely 
if their __get() was written improperly. 

Reproduce code:
---------------
class test
{
    protected $_a = 6;

    function __get($key) {
        if($key == 'stuff') {
            return $this->stuff();
        } else if($key == 'a') {
            return $this->_a;
        }
    }

    function stuff()
    {
        return array('random' => 'key', 'using_getter' => 10 * $this->a);
    }
}

$test = new test();
print 'this should be 60: '.$test->stuff['using_getter'].'<br/>';
print 'this should be 6: '.$test->a.'<br/>';                            

Expected result:
----------------
this should be 60: 60 
this should be 6: 6 

Actual result:
--------------
this should be 60: 0 
this should be 6: 6 
 
Also, note, this warning is raised: 
 
[[ Undefined property:  test::$a ]] 
on /var/www/html/test.php 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-04 22:27 UTC] tony2001@php.net
>If this is to keep recursion from happening
Yes.

>A programmer can ALWAYS shoot himself
But not THAT easy.
You can encounter infinite recursion every time you try to read non-existent object property in __get(). 

This is by design and won't be changed.
 [2015-12-16 23:07 UTC] a dot teal at warwick dot ac dot uk
Nice one guys, a decade of this.

I don't understand how you lot can be so thick! Why would you stop recursion for this one thing. Seriously who thought "lets waste resources implementing this".

It's like you hate developers. I hate playing "guess if the function has underscores"  or "guess the argument order" or "false, null or -1" and now we have this!
 [2015-12-17 00:00 UTC] a dot teal at warwick dot ac dot uk
Not sure what you're showing, but can confirm in 5.5.9 and 5.5.11 I get "undefined property" when it recurses back into __get
 [2015-12-17 00:17 UTC] requinix@php.net
Here's a more annotated version: https://3v4l.org/gbBH9
It clearly shows that __get is being called more than once. And the output is as expected.

If you're having problems of your own then create a new bug report instead of hijacking one from 2005.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 15:01:48 2019 UTC