Make PHP Object do different actions depending on variable name


Make PHP Object do different actions depending on variable name



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?




Exposing a same class twice using WCF

1:



Python: How do I make a subclass from a superclass?
Even if it's possible (I don't think so), you shouldn't.


PHP, create_function or evalute it at runtime?
Others would just see a lot of magic and it would be hard to debug.


How do you find all implementations of an interface, in code?
Stick to the standards and known ideas / patterns whenever possible..
Declaring instance variables iterating over a hash!
Why don't you want to use:.
How to make class not derivable at all. is there any way?
$user = new UserData(24); 
Or even better (because you shouldn't do any blocking operations in the constructor):.
PHP Class arguments in functions
$user = UserData::getById(24); 


Is it possible to save arbitrary data to a file in C#?

2:


Since a given object may have multiple names, this is not a generally valid technique in modern programming languages.

For example, assuming user 25 has a different name from user 24, what would you expect the following code to print?.
$user24 = new UserData; echo 'Welcome, '.$user24->name.'!'; $user25 = $user24; echo 'Welcome, '.$user25->name.'!'; 
Not only that, but you can have objects with no name:.
echo 'Welcome, '.(new UserData)->name.'!'; 
A more typical implementation would have the object constructor take a parameter which tells it which user you're dealing with, like this:.
$user = new UserData(24); echo 'Welcome, '.$user->name.'!'; 


3:


A value (such as an object)'s relation to its variable name is one-to-many - that is, one value may have many names, where each of them is a reference to the same value.

It may even have no name at all, ie, it could be an expression (a new expression, for example, returns an object).

Therefore it is not possible to find out "the" name of a value programmatically.

A value could have multiple names, or no names..



70 out of 100 based on 70 user ratings 370 reviews