Get parent extends class in php?

Your code suggested you used parent which in fact is what you need. The issue lies with the magic CLASS variable.

Up vote 12 down vote favorite share g+ share fb share tw.

I have the oop php code: class a { // with propertie and functions } class be extends a { public function test() { echo __CLASS__; // this is be // parent::__CLASS__ // error } } $b = new b(); $b->test(); I have a few parent class (normal and abstract) and many child classes. The child classes extend the parent classes. So when I instantiate the child at some point I need to find out what parent I called.

For example the funciton b::test() will return a How can I get (from my code) the class a from my class b? Thanks php class parent extend link|improve this question edited Nov 26 '11 at 14:38Hunter McMillen4,2001311 asked Nov 26 '11 at 14:35ana50912 83% accept rate.

3 read this: stackoverflow.com/questions/506705/… – Book Of Zeus Nov 26 '11 at 14:43.

Your code suggested you used parent, which in fact is what you need. The issue lies with the magic __CLASS__ variable. The documentation states: As of PHP 5 this constant returns the class name as it was declared.

Which is what we need, but as noted in this comment on php. Net: claude noted that __CLASS__ always contains the class that it is called in, if you would rather have the class that called the method use get_class($this) instead. However this only works with instances, not when called statically.

If you only are in need of the parent class, theres a function for that aswell. That one is called get_parent_class.

You can use get_parent_class: class A {} class B extends A { public function test() { echo get_parent_class(); } } $b = new B; $b->test(); // A This will also work if B::test is static. NOTE: There is a small difference between using get_parent_class without arguments versus passing $this as an argument. If we extend the above example with: class C extends B {} $c = new C; $c->test(); // A We get A as the parent class (the parent class of B, where the method is called).

If you always want the closest parent for the object you're testing you should use get_parent_class($this) instead.

Class a { // with propertie and functions } class be extends a { public function test() { echo get_parent_class($this); } } $b = new b(); $b->test().

You can use reflection to do that: Instead of parent::__CLASS__; use $ref = new ReflectionClass($this); echo $ref->getParentClass()->getName().

Use class_parents instead. It'll give an array of parents. '); } } $c = new C; $c->test(); // B -> A.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions