PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I don't know if this is possible, but I'm hoping it is. I'm trying to get an objects run-time variable name from within itself. For example, if I have this code:
class foo
{
public $firstName = "Billy";
public $lastName = "Bob";
function __construct()
{
// how can i get the variable name 'myFooInstance' from here?
}
}
$myFooInstance = new foo();
The only solution I can think of is to pass each class it's unique variable name, like this:
I'm trying to create loadViewState() and saveViewState() methods that would apply to every object. These would actually be called in each objects __construct() and __destruct() methods. However, I need each instance of an object to have a unique ID for this to work. I was hoping to make it automatic, but I may have to manually assign each object a unique ID.
its possible, using debug backtrace.
possible doesn't mean sensible though.
stick with the string in the constructor there is nothing wrong with that or if you want to create an internal id that the user of the class is unaware of use a static property like this:
I found a use for it the other day. I'm writing a form management package and I wanted to generate ids attribute for all the fields, this was perfect. I ditched it in the end in favor of forcing the user of the package to specify their own