PHP: Object substitution in constructor

I’m struggling with how to get the name of an instance of a class within a method on an object of that class. From the outside, everything goes well, but inside the variable name determination function does not work. From here also there was a question whether it is possible to redefine the generated object in the constructor? Also, is using eval() bad form?<br>
<b>UPD:</b><br>
Surprisingly, part of the answer to this question was suggested in another topic: <a href=”http://hashcode.ru/questions/54755/”>Calling a function in a chain after the constructor</a> <br>
In principle, the task is almost solved, with one exception, for complete convenience, the following is missing: how to call the destructor immediately after the constructor fires or call the function in a chain after the constructor (there are no problems with the usual class method)?


Answer 1, authority 100%

I didn’t understand your question exactly, but perhaps you are looking for the “magic” constant __CLASS__?

Also in php 5.3+ there is a function get_called_classwhich inside any class and any method can determine the name of the class that called it.

PS: you definitely shouldn’t write such horror as get_class($this)


Answer 2

get_class($this) will return the name of the class within the class,
get_class($obj) will return the class name of the passed object in external code.
those. in constructor

public function __consruct(){
    $this->_className = get_class($this);
}

will pass $this->_className to the class name. According to the same scheme, get_parent_class() – works with the name of the parent class.

IMHO it’s better to figure out the right functions than to sculpt crutches