As title, $a
is Class A, and call foo function.
$a->foo();
However, Class A has many subclasses, and sub-subclasses. And some of them are using dozens of traits, implementing many interfaces. I'm not sure which subclass $a
is. My question is, how could I know which foo function is called? I definitely can call foo() by using incorrect params,
$a->foo('error');
And I'm gonna get error trace stack. But how can I obtain the class name or trait name directly? Thanks in advance!
Check this example:
<?php
class Foo
{
protected $who;
public function printItem($string)
{
echo 'printItem (' . __CLASS__ . '): ' . $string . PHP_EOL;
$this->who = __CLASS__;
}
public function getClass()
{
echo $this->who . PHP_EOL;
}
}
class Bar extends Foo
{
public function printItem($string)
{
echo 'printItem (' . __CLASS__ . '): ' . $string . PHP_EOL;
$this->who = __CLASS__;
}
}
$a = new Foo();
$b = new Bar();
$a->printItem('baz'); // Output: 'printItem (Foo): baz'
$a->getClass(); // Output: Foo
$b->printItem('baz'); // Output: 'printItem (Bar): baz'
$b->getClass(); // Output: Bar
?>
You can read more in:
Here is the demo ,try this
<?php
class myclass {
function myclass() {
return(true);
}
function myfunc1(){
return(true);
}
function myfunc2(){
return(true);
}
}
$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) {
echo "$method_name
";
}
// output :myclass myfunc1 myfunc2