Get all defined classes of a parent class in php
If you need that, it really smells like bad code, the base class shouldn't need to know this.
However, if you definitions have been included (i.e. you don't need to include new files with classes you possibly have), you could run:
$children = array();
foreach(get_declared_classes() as $class){
if($class instanceof foo) $children[] = $class;
}
PHP OOP get all parent classes of a class
This would do:
function GetAllParents(instance) {
return get_class(instance) . '<-' .
implode('<-', array_reverse(class_parents(instance)));
}
Outputs all in the right order:
Mercedes<-Car<-Main
See documentation:
- http://php.net/class_parents
- http://php.net/implode
Get all extended Classes in PHP
class ObjectModel {
}
class SomeNewClass extends ObjectModel {
}
class SomeOtherNewClass extends ObjectModel {
}
class SomeOtherNewClassLol extends ObjectModel {
}
function get_extends_number($base){
$rt=0;
foreach(get_declared_classes() as $class)
if(is_subclass_of($class,$base)) $rt++;
return $rt;
}
echo get_extends_number('ObjectModel'); //output: 3
Yes, you can do it, DEMO
PHP OOP: Get all declared classes which are have extended another parent class
Your solution seems fine, though I'm not sure why you'd do this. There is no easy way in php to say, 'give me all the declared classes of a certain parent class globally' without actually checking globally each declared class. Even if you have a couple hundred classes loaded to loop through, it shouldn't be too heavy as they're all in memory.
If you're trying to just track loaded child classes for a specific parent, why not create a registry that tracks them when they're loaded? You could do this tracking in an autoloader or factory used for the child classes or event as a hack, just by putting something at the top of the class file before the class definition.
how to obtain all subclasses of a class in php
function getSubclassesOf($parent) {
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent))
$result[] = $class;
}
return $result;
}
Coincidentally, this implementation is exactly the one given in the question linked to by Vadim.
Getting the name of a child class in the parent class (static context)
in short. this is not possible. in php4 you could implement a terrible hack (examine the debug_backtrace()
) but that method does not work in PHP5. references:
30423
37684
34421
edit: an example of late static binding in PHP 5.3 (mentioned in comments). note there are potential problems in it's current implementation (src).
class Base {
public static function whoAmI() {
return get_called_class();
}
}
class User extends Base {}
print Base::whoAmI(); // prints "Base"
print User::whoAmI(); // prints "User"
Get PHP Class names of classes that extend a class?
Yes, you can do through Reflection:
$children = array();
foreach( get_declared_classes() as $class )
{
$reflected = new ReflectionClass( $class );
if( $reflected->isSubclassOf( 'Plugins' ) ) $children[] = $class;
}
Getting the name of a child class in PHP
A base class should really never depend on information about child classes---
To answer your question:
class base {
public function __construct() {
print "Class:" . get_class($this) . "\n";
}
}
class child extends base{
public function __construct() {
parent::__construct();
}
}
$c = new child();
Just for future reference -- this can be acheived in a static context using get_called_class(), but this is only available in PHP >= 5.3
Related Topics
How to Convert Object into String in PHP
Redirecting Wordpress's Login/Register Page to a Custom Login/Registration Page
Copy File from Remote Server or Url
How to Set Current Page "Active" in PHP
Create New Product Attribute Programmatically in Woocommerce
Running Imagemagick on Xampp 1.8.3 with Windows 7
Windows Cmd.Exe "The System Cannot Find the Path Specified."
Phpmysql Error - #1273 - #1273 - Unknown Collation: 'Utf8Mb4_General_Ci'
PHP Directory List from Remote Server
Inserting Now() into Database with Codeigniter's Active Record
How to Send Cookies with File_Get_Contents
How to Tell If a Timezone Observes Daylight Saving at Any Time of the Year