$this return calling object.

php.internals

Jochem Maas

22 years ago
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(); ?>

Ard Biesheuvel

22 years ago
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