Is there a way for a class called inside another to know the name of the outer class?
Example:
class A{
// need to get the name of B
// some stuff
}
class B{
$var = new A;
}
get_parent_class() qon’t do the work since B is not a child of A.
Any suggestions?
Edit: Sorry everyone I had to change the question. What I wanted to ask is can class A know the name of the class in which it is being called. Again sorry for idiotic first question.
Nope, it’s impossible.
Well, almost. You could hack your way to the calling class from debug_backtrace or apd_callstack, but that is rather inefficient and slow. Xdebug has a number of functions that could help you achieve this too, but all of this is nothing you want in production code.
The easiest way would be to pass the instance of B to A, e.g.