Reputation: 5312
I was experimenting with php, trying to get the class of a class property.
class A{
public $a;
}
class B extends A{
public $b;
}
class C extends B{
public $c;
}
$c = new C();
echo get_class($c); // C
echo get_class($c->a); // false
echo get_class($c->b); // false
echo get_class($c->c); // false
Using the get_class
method I can get "C" from $c,
but with the properties $c->a, $c->b, $c->c that function returns "false".
Question
How can I get the class the properties belong to? ..like:
echo foo($c->a); // A
echo foo($c->b); // B
echo foo($c->c); // C
is it possible?
Upvotes: 2
Views: 53
Reputation: 4584
get_class() to get current class name
and get_parent_class() to get parent or extended class
echo get_class($c);
$b = get_parent_class($c);//this will get extended class B
echo $b;
echo get_parent_class(new $b());//$b="B" then we call new B class to get parent class 'A'
Upvotes: 1