note 80341 added to function.get-class

From: Date: Thu, 10 Jan 2008 20:51:23 +0000
Subject: note 80341 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-135012@lists.php.net to get a copy of this message
I may be missing something, but in all the laments wishing for some sort of get_real_class() function to return the final class name of of a derived class (a.k.a. subclass), my testing suggests that the existing get_class() function can do this just fine, at least when an object of the derived class has been instantiated. Consider: <?php class BaseClass { function whoAmI() { return get_class($this); } } class DerivedClass extends BaseClass {} $baseInstance = new BaseClass(); $derivedInstance = new DerivedClass(); echo $baseInstance->whoAmI(); // returns "BaseClass" echo $derivedInstance->whoAmI(); // returns "DerivedClass" echo BaseClass::whoAmI(); // returns an empty string echo DerivedClass::whoAmI(); // returns an empty string ?> In my opinion, the final two calls in my example should actually throw runtime errors, but apparently they don't. Now consider: <?php class BaseClass { function whoAmI() { return get_class(/* $this */); } } class DerivedClass extends BaseClass {} $baseInstance = new BaseClass(); $derivedInstance = new DerivedClass(); echo $baseInstance->whoAmI(); // returns "BaseClass" echo $derivedInstance->whoAmI(); // returns "BaseClass" echo BaseClass::whoAmI(); // returns "BaseClass" echo DerivedClass::whoAmI(); // returns "BaseClass" ?> So you can see, if you need to know the final class name of an *object* constructed from a subclass there's no problem, though if what you're after is the final class name of a derived class *for which you have no instance handy* then what I've demonstrated here isn't going to help you with that -- though it's hard for me to imagine you could write code like DerivedClass::whoAmI() and seriously care about the value you get back, since this line of code itself shows that you know the final class name of the derived class already to begin with! But people are clever, so maybe there is some case where my simple view on this breaks down. Anyway, happy trails. I hope this helps someone someday. ---- Server IP: 208.69.120.35 Probable Submitter: 65.207.72.66 ---- Manual Page -- http://www.php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/80341 Del: integrated -- https://master.php.net/note/delete/80341/integrated Del: useless -- https://master.php.net/note/delete/80341/useless Del: bad code -- https://master.php.net/note/delete/80341/bad+code Del: spam -- https://master.php.net/note/delete/80341/spam Del: non-english -- https://master.php.net/note/delete/80341/non-english Del: in docs -- https://master.php.net/note/delete/80341/in+docs Del: other reasons-- https://master.php.net/note/delete/80341 Reject -- https://master.php.net/note/reject/80341 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135012) next »