note 106794 added to function.get-class

From: Date: Tue, 06 Dec 2011 22:51:12 +0000
Subject: note 106794 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-184963@lists.php.net to get a copy of this message
People seem to mix up what __METHOD__, get_class($obj) and get_class() do, related to class inheritance. Here's a good example that should fix that for ever: <?php class Foo { function doMethod(){ echo __METHOD__ . "\n"; } function doGetClassThis(){ echo get_class($this).'::doThat' . "\n"; } function doGetClass(){ echo get_class().'::doThat' . "\n"; } } class Bar extends Foo { } class Quux extends Bar { function doMethod(){ echo __METHOD__ . "\n"; } function doGetClassThis(){ echo get_class($this).'::doThat' . "\n"; } function doGetClass(){ echo get_class().'::doThat' . "\n"; } } $foo = new Foo(); $bar = new Bar(); $quux = new Quux(); echo "\n--doMethod--\n"; $foo->doMethod(); $bar->doMethod(); $quux->doMethod(); echo "\n--doGetClassThis--\n"; $foo->doGetClassThis(); $bar->doGetClassThis(); $quux->doGetClassThis(); echo "\n--doGetClass--\n"; $foo->doGetClass(); $bar->doGetClass(); $quux->doGetClass(); ?> OUTPUT: --doMethod-- Foo::doMethod Foo::doMethod Quux::doMethod --doGetClassThis-- Foo::doThat Bar::doThat Quux::doThat --doGetClass-- Foo::doThat Foo::doThat Quux::doThat (fork at http://codepad.org/q6RWVPrW) ---- Server IP: 69.147.83.197 Probable Submitter: 82.74.192.60 ---- Manual Page -- http://www.php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/106794 Del: integrated -- https://master.php.net/note/delete/106794/integrated Del: useless -- https://master.php.net/note/delete/106794/useless Del: bad code -- https://master.php.net/note/delete/106794/bad+code Del: spam -- https://master.php.net/note/delete/106794/spam Del: non-english -- https://master.php.net/note/delete/106794/non-english Del: in docs -- https://master.php.net/note/delete/106794/in+docs Del: other reasons-- https://master.php.net/note/delete/106794 Reject -- https://master.php.net/note/reject/106794 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184963) next »