|
Posted by "Christopher J. Bottaro" on 05/23/05 01:24
Marek Kilimajer wrote:
> Christopher J. Bottaro wrote:
>> Jochem Maas wrote:
>>
>>
>>>Christopher J. Bottaro wrote:
>>>
>>>>Maybe I'm using "reentrant" incorrectly, but here is what I mean...
>>>>
>>>>class Test {
>>>> function __get($nm) {
>>>> if ($nm == 'x')
>>>> return $this->func();
>>>> elseif ($nm == 'y')
>>>> return 'y';
>>>> elseif ($nm == 'xx')
>>>> return 'x';
>>>> }
>>>> function func() {
>>>> return $this->xx;
>>>> }
>>>>}
>>>>$t = new Test();
>>>>print $t->y . "\n";
>>>>print $t->xx . "\n";
>>>>print $t->x . "\n";
>>>>print $t->func() . "\n";
>>>>
>>>>I would expect the following code to output:
>>>>y
>>>>x
>>>>x
>>>>x
>>>>
>>>>But instead, it outputs:
>>>>y
>>>>x
>>>>
>>>>x
>>>>
>>>>Is this a bug? This limitation is not documented (maybe it should be?).
>>>
>>>its not a bug, I believe its documented somewhere how this works.
>>>bottom line __get() does not work from 'inside' the class/object,
>>>so do something like instead:
>>>
>>> function func() {
>>> return $this->__get('xx');
>>> }
>>>
>>>which may not please the soul, but does work ;-)
>>
>>
>> Hehe, my soul is hard to please...=P
>>
>> Actually, __get() does work from inside the class. In the sample code I
>> posted, func() does indeed return 'x' when called from main. It does not
>> work when called from within a call to __get(). In other words,
>> $this->attribute does not work if __get() appears anywhere in the call
>> stack.
>>
>> Its just a small annoyance.
>
> I think it would be more annoying if __get() would be recursively called
> to infinity.
And what would make it any different from a normal recursive function?
Every recursive function runs the risk of going into infinite loop if the
programmer doesn't understand the basic concept (or makes a silly mistake).
Loops run the risk of going on indefinitely as well. Maybe PHP should
disable all forms of loops/recursion to protect the programmers from
themselves.
Trace my code for $t->x...
$t->x;
$t->__get('x');
$t->func();
$t->xx;
$t->__get('xx');
'x'
What is wrong with that? Why should PHP disallow that recursive __get()
call? It is perfectly valid recursive code. It terminates for all cases.
-- C
[Back to original message]
|