# $this return calling object.

- Thread: https://externals.io/message/9642
- Original archive: https://news-web.php.net/php.internals/9642
- Started by Jochem Maas on 2004-05-03T15:50:02+00:00
- Messages: 2
- 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

---

## #9642 — Jochem Maas — 2004-05-03T15:50:02+00:00

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

I have discovered behaviour which I imagine is not intended, name 
referencing $this in a static method that is called from a from the 
method of an instantiated. (see test code below.)

anyone have an explaination for $this is referencing the calling object 
(at least in the build I'm using)?

I'm using PHP Version 5.0.0RC3-dev on an i686 Linux box:
PHP API  20031224
PHP Extension  20040412
Zend Extension  220040412
Configure Command
'../php5-cvs/configure' '--with-interbase=shared,/opt/firebird' 
'--prefix=/usr' '--with-regex=system' 
'--with-config-file-path=/etc/php5/apache' '--enable-calendar' 
'--enable-ftp' '--with-gettext=shared' '--enable-sysvsem' 
'--enable-sysvshm' '--enable-trans-sid' '--enable-debug' 
'--disable-static' '--with-pcre-regex' '--enable-sockets' 
'--with-zlib-dir=/usr' '--enable-wddx=shared--with-imap=shared,/usr' 
'--with-kerberos=/usr' '--with-gd=shared,/usr' '--with-jpeg-dir=/usr' 
'--with-png-dir=/usr' '--with-freetype-dir=/usr' 
'--with-zlib=shared,/usr' '--with-xsl=shared,/usr' 
'--with-apxs=/usr/bin/apxs' '--with-interbase=/opt/interbase' 
'--with-interbase=/opt/interbase' '--with-gd=shared'


thanks in advance, sorry if this is misplaced.
-----

<?php

error_reporting(E_ALL & E_STRICT);

Class A {
     function test() {
         print_r( __CLASS__ );echo "\n";
         print_r( __METHOD__ );echo "\n";
         print_r( $this );echo "\n";
     }
}

Class B { function test() { A::test(); } }

$b = new B();
$b->test();

?

---

## #9643 — Ard Biesheuvel — 2004-05-03T16:19:17+00:00

URL: https://externals.io/message/9642#9643 · In reply to: #9642

Ha Jochem,

Jochem Maas wrote:
> I have discovered behaviour which I imagine is not intended, name 
> referencing $this in a static method that is called from a from the 
> method of an instantiated. (see test code below.)

> error_reporting(E_ALL & E_STRICT);

I'm sure you mean `E_ALL` | `E_STRICT` here ...

> 
> Class A {
>     function test() {
>         print_r( __CLASS__ );echo "\n";
>         print_r( __METHOD__ );echo "\n";
>         print_r( $this );echo "\n";
>     }
> }
> 
> Class B { function test() { A::test(); } }
> 

This behaviour is intentional for methods that are not defined as 
static. Also, as inherited methods are called using the parent:: syntax, 
this is the only way to call overridden methods without losing $this 
from scope along the way.

-- 
Ard
