note 57780 deleted from function.get-class by mazzanet

From: Date: Fri, 14 Oct 2005 05:44:42 +0000
Subject: note 57780 deleted from function.get-class by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96996@lists.php.net to get a copy of this message
Note Submitter: taylorbarstow at that google mail thingy ---- Interesting behavior in PHP5; if you don't pass $this from within an object method, it returns the name of the current class; if you do pass $this, it returns the name of the class of the object (which might not be the current class due to inheritance). <?php class MyParent { function whoami() { echo 'Without passing $this, we have: ' . get_class() . "\n"; echo 'Passing $this, we have: ' . get_class($this) . "\n"; } } class MyChild extends MyParent { } $c = new MyChild; $c->whoami(); ?> Output: Without passing $this, we have: MyParent Passing $this, we have: MyChild So, get_class() without $this is like __CLASS__.

« previous php.notes (#96996) next »