# call_user_func, static calls, and $this

- Thread: https://externals.io/message/3873
- Original archive: https://news-web.php.net/php.internals/3873
- Started by Brad Bulger on 2003-08-13T06:40:37+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3873 — Brad Bulger — 2003-08-13T06:40:37+00:00

URL: https://externals.io/message/3873#3873

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 :)

---

## #3875 — Brad Bulger — 2003-08-13T07:34:20+00:00

URL: https://externals.io/message/3873#3875 · In reply to: #3873

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.

---

## #3877 — Zeev Suraski — 2003-08-13T08:37:52+00:00

URL: https://externals.io/message/3873#3877 · In reply to: #3873

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
