Can a function/class know the context from where it is being invoked or instantiated?
- by vrode
Let's take this class as example and assume that get_context() returns the source of the call:
class A {
public function __construct( ) {
if( get_class( get_context( ) ) == B ) {
return true;
} else {
return false;
}
}
}
class B {
function __construct( ) {
$a = new A( );
}
}
$a = new B( ); // returns true, as B is the invoking class of A
$a = new A( ); // returns false, as B is invoked outside of any class
So, my questions are:
1) can a function know the context that calls it?
2) can a object know context from where it has been instantiated?
Or am I dreaming up new features not implementable in PHP?