Bug #33998 [Nab]: Calling a getter within a getter does not work.
Edit report at https://bugs.php.net/bug.php?id=33998&edit=1
ID: 33998
Updated by: requinix@php.net
Reported by: dan at stratitec dot com
Summary: Calling a getter within a getter does not work.
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux (Red Hat 9)
PHP Version: 5.0.3
Block user comment: N
Private report: N
New Comment:
https://3v4l.org/YZHgS
Previous Comments:
------------------------------------------------------------------------
[2015-12-16 23:07:35] 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!
------------------------------------------------------------------------
[2005-08-04 22:27:54] 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.
------------------------------------------------------------------------
[2005-08-04 22:22:07] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=33998&edit=1
Thread (6 messages)