Can a function/class know the context from where it is being invoked or instantiated?

Posted by vrode on Stack Overflow See other posts from Stack Overflow or by vrode
Published on 2010-05-15T01:29:09Z Indexed on 2010/05/15 1:34 UTC
Read the original article Hit count: 164

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about context