If I have code like this:
class Person {
$age;
$height;
$more_stuff_about_the_person;
function about() {
return /* Can I get the person's name? */;
}
}
$John = new Person();
$Peter = new Person();
print $John->about(); // Print "John".
print $Peter->about(); // Print "Peter".
Is it possible to print the person's name, stored as the variable name, from the method?
As it's not standard procedure, I'm guessing it's a bad idea.
I've looked it up and I can't find anything about it.
Personnot a have an instance variable$name? What if you have objects in an array like$p = array(new Person(), new Person())?debug_backtrace()(which will give you the line theabout()call was made from) and the tokenizer (which may give you the variable reference the call was made for) but it's not really suitable for production use${"Maureen O'Hara"} = new StdClass; $firstName = 'Maureen'; $lastName = "O'Hara"; var_dump( ${"$firstName $lastName"} );:D