this is old subject everyone is tired of i'm sure. but since large changes
have been made to call_user_func[_array] recently, i want to confirm:
if an object calls one of its methods ($thing->foo()), any static-type
method calls made inside there - self::a(), parent::a(), fezbar::a() -
will have $this defined, pointing to original calling object.
self:: and parent:: are always relative to the containing class,
regardless of this.
two exceptions:
-
the method is declared as 'static'; if so, $this is never set
-
the method is called via call_user_func or call_user_func_array;
if so, $this is not set
the very last part seems like it might be a bug, or it might be a
decision - that call_user_func(array('class','function'),$arg)
will always force class::function() to behave as if declared statically.
that is a bit of a problem for self:: and parent:: but that is ok if it
is how it must be.
if anyone can say about this, it would be kind.
ps - i looked in old mail, sure this had been discussed - topic came up,
but nothing for sure, except introduction of 'static' to prevent $this,
which solves opposite problem :)
semi-related: __call is somewhat reversed. it will catch
call_user_func(array('self','fake')), but not self::fake()
(same for parent vs parent:: or randomclass vs randomclass::)
the visibility of $this is not changed, so you end up with
no $this inside __call. it can be a surprise.
At 09:40 13/08/2003, Brad Bulger wrote:
if an object calls one of its methods ($thing->foo()), any static-type
method calls made inside there - self::a(), parent::a(), fezbar::a() -
will have $this defined, pointing to original calling object.
self:: and parent:: are always relative to the containing class,
regardless of this.two exceptions:
the method is declared as 'static'; if so, $this is never set
the method is called via call_user_func or call_user_func_array;
if so, $this is not set
This should be fixed. $this should be visible for methods invoked through
call_user_function().
And you're absolutely right about your analysis of the ::
operator. Contrary to popular belief it was NOT introduced for static
function calls, but rather, for being able to invoke methods of parent
classes (a much more common use for the :: operator in other languages too).
Zeev