Is there a way to get the actual calling object from a method?
For example, I have a scenario where I do something like this:
$user = new User;
$user->setName('name');
$user->save($user);
Instead of passing the $user to the save method, it would be nice to just have it get the calling object information.
I know I can do get_called_class() but that doesn’t return the actual object’s data. I was also looking at PHP’s Reflection classes but didn’t see anything exactly right.
Any ideas?
Thanks!
Within your object (and thus from within the
savefunction itself, you can utilize the$thiskeyword to access the object itself. Alternatively…Where
instancewould be a private member variable. Make sure your constructor is private so that your object cannot be instantiated externally. <— all this assuming php5.