is it possible to get the name of the object which calls one of its methods.
scenario:
I have class A. I instantiate 2 objects of that class. If one object calls a method, is it possible to retrieve the name of the object which called it?
EDIT:
class Property() {
public function __call($name, $atts) {
if ($name === 'foo') {
//I want to differ Between Color and Position
}
}
}
$Color = new Property();
$Position = new Property();
$Color->foo();
$Position->foo();
Add a name element to your object:
class ObJA {
$this->name;
function __construct($name){
$this->name = $name;
}
function getName(){ return $this->name; }
}
On object create:
$a = new ObJA('a');
$b = new ObJA('is b');
echo $a->getName(); //`a`
echo $b->getName(); //`is b`
You can always use get_class()
, but the name is not going to change simply by creating two or more instances of the object. Neal's solution will work but it doesn't actually change the name of the class, and begs the question: Why do you need it?