For example, if let’s say I have a database of users. I have a class that gets information form the database about these users.
I want to make the class “self aware” of it’s own name, like this:
<?php
class UserData {
//[code that fetches the ID number from the variable name, and queries the database for info]
}
$user24 = new UserData;
echo 'Welcome, '.$user24->name.'!';
?>
This code would, ideally, output something like “Welcome, Bob!”, and would change depending on what I named it. Is this possible?
Even if it’s possible (I don’t think so), you shouldn’t. Others would just see a lot of magic and it would be hard to debug. Stick to the standards and known ideas / patterns whenever possible.
Why don’t you want to use:
Or even better (because you shouldn’t do any blocking operations in the constructor):