note 97313 modified in language.oop5.static by danbrown

From: Date: Wed, 14 Apr 2010 12:20:08 +0000
Subject: note 97313 modified in language.oop5.static by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168226@lists.php.net to get a copy of this message
If you inadvertently call a non-static method in one class from another class, using $this in the former will actually refer to the wrong class. <?php class CalledClass { function go() { print(get_class($this) . "\n"); return true; } } class CallerClass { function go() { CalledClass::Go(); return true; } } $obj = new CallerClass(); $obj->go(); // Output is "CallerClass" instead of "CalledClass" like it should be. ?> Dustin Oprea --was-- If you inadvertently call a non-static method in one class from another class, using $this in the former will actually refer to the wrong class. class CalledClass { function go() { print(get_class($this) . "\n"); return true; } } class CallerClass { function go() { CalledClass::Go(); return true; } } $obj = new CallerClass(); $obj->go(); // Output is "CallerClass" instead of "CalledClass" like it should be. Dustin Oprea http://php.net/manual/en/language.oop5.static.php

« previous php.notes (#168226) next »