[PHP] How to find the object name in a class?
I've been using a function to output HTML to the user's browser, and now my use has made it neccessary to put it all inside a class. All's going find and dandy and I've got it all working. However one of it's variables is $front_page. Now in other pages there are references to $front_page, and I want to change them to something like $objectname->front_pagee. The problem is that I can't know what the object will be named ahead of time! So my hack-around is in the class constructor I set a global variable called $pge with the value submitted to the constructor. So someone would do this: $somename = new Display(somename); But if someone typos the and the name of the variable is different from the one submitted to the constructor, all the code will break! That's because I'm using a variable variable to refer to $front_page, like this: ${$pge}->front_page I told you it was a hack-around, and it isn't a good one. So is there ANY way to get the name of the object in PHP code without knowing the name of the object ahead of time? I'm really stumped! -- Plutarck Should be working on something... ...but forgot what it was. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] How to find the object name in a class?
> So is there ANY way to get the name of the object in PHP code without > knowing the name of the object ahead of time? I went through this exact thing not too long ago with an error class I wrote. Unfortunately, there is no way to know. What you can do, however, is do a check to see if that object exists before you access the variable. Other than that, I think you're outta luck. :/ Chris
Re: [PHP] How to find the object name in a class?
*sigh* I'm thinking so too ;( Doh! However, I have at least a usable hack around. In the constructor of your class, add: function Class ($object_name) { global $pge; $pge = $object_name; } Then when you use create a new object of that type you must use: $objectname = new Class('objectname'); Then in your code you just use ${$pge}-> to refer to it. Sucks, doesn't it? -- Plutarck Should be working on something... ...but forgot what it was. ""Boget, Chris"" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > So is there ANY way to get the name of the object in PHP code without > > knowing the name of the object ahead of time? > > I went through this exact thing not too long ago with an error class > I wrote. Unfortunately, there is no way to know. What you can do, > however, is do a check to see if that object exists before you access > the variable. Other than that, I think you're outta luck. :/ > > Chris > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] How to find the object name in a class?
> *sigh* I'm thinking so too ;( > Doh! > However, I have at least a usable hack around. > In the constructor of your class, add: > function Class ($object_name) { > global $pge; > $pge = $object_name; > } > Then when you use create a new object of that type you must use: > $objectname = new Class('objectname'); Heh. This is exactly what I'm doing as it was the only work around that I could find, too. > Then in your code you just use ${$pge}-> to refer to it. Yup. However, before I do this, I check to make sure $pge is valid before I use it. Otherwise, all kinds of nastiness can occur. What I'm doing this for is my error reporting class. I'm using a wrapper function that calls my class' errorHandler() method since you cannot specify a class method as the handler function. Kind of funky. But doing this was the only way I could get around it. > Sucks, doesn't it? Yup. But I'm sure there is a reason for it. Kind of like whatever the reason is that we cannot actually get a variable's name. I.E. $joe = "bob"; we cannot get that the above variable's name is "joe". Oh, well. Chris