Is There a Way to Examine Calling Class / Object From Inside Called Method?

I can’t find anything in the documentation to allow a Class to interrogate the object calling it.

An example of where this would be helpful is when using the magic __get() & __set() overload methods. Say you want to keep your member variables Protected, but you want to avoid having to hard code getters and setters for EVERY variable.


class Test 
{
   protected $var, $moreVars, $EvenMoreVars....;

   public function __set($key, $value)
   {
         if( /* Check if object attempting to set this variable is my child */)
                  $this->$key = $value;
         else
                  echo 'You are outside of scope';
    }

   public function __get($var)
   {
         if(/* Check if object attempting to get this variable is my child */)
                   return $this->$var;
         else
                  echo 'You are outside of scope';
    }
}

Umm…yeah you are unnecessary making this complicated. No form of Object-Oriented Language provides that kind of feature because you are not suppose to care what called your object. Putting in hard dependencies like that is the bane of Object-Oriented Programming. Furthermore, I assume when you say “is my child” it Extends from Test in which case in can just use “$this->property” and will not go though the setter or getter.

If you don’t want other parts of your code to access properties of an object, don’t make them accessible.