To pillepop2003 at yahoo dot de:
I have the same issue. I have a base class that manages database tasks for a number of child classes. One of the functions in the base class is a find() method that returns instances of the child classes. Since find() is usually called as a static method, it needs to know the name of the child class. As you've found, this appears to be impossible to get in an easy fashion.
The only way I've found to get the child class name is to use the debug_traceback() function. This requires me to have a find() method in every child class, but it does work.
Here's an example:
<?php
require_once("Application.php");
class parentClass {
function find() {
$className = NULL;
foreach (debug_backtrace() as $bt) {
if ($bt['function'] == __FUNCTION__) {
$className = $bt['class'];
}
}
$id = 1;
return new $className($id);
}
}
class foo extends parentClass {
function __construct($id) {
$this->id = id;
}
function find() {
return parent::find();
}
}
class bar extends parentClass {
function __construct($id) {
$this->id = id;
}
function find() {
return parent::find();
}
}
$a = foo::find();
printf("Type for \$a: %s<br/>\n", get_class($a));
$b = bar::find();
printf("Type for \$b: %s<br/>\n", get_class($b));
?>