oop - Can a PHP object instance know its name? -
if have code this:
class person { $age; $height; $more_stuff_about_the_person; function about() { return /* can person's name? */; } } $john = new person(); $peter = new person(); print $john->about(); // print "john". print $peter->about(); // print "peter".
is possible print person's name, stored variable name, method?
as it's not standard procedure, i'm guessing it's bad idea.
i've looked , can't find it.
no. objects can have multiple names, or no names. happen here:
$john = new person(); $richie = $john; // $john , $richie both refer same object. print $richie->about();
or here:
function f($person) { print $person->about(); } f(new person());
if objects need know own names, need explicitly store names member variables (like $age
, $height
).
Comments
Post a Comment