calcop Posted June 5, 2006 Share Posted June 5, 2006 Hello everyone!I was hoping someone could help me with my problem. I have been reading over the PHP manual, but cannot find the answer to my question.I need to get the variable name of a declared class. For example, I need to be able to do something such as:[code]$MyNewClassObject = new SideBar;$MyNewClassObject->getVarName ();[/code]getVarName should return "MyNewClassObject";I thought I had found the answer with [!--coloro:#33CC00--][span style=\"color:#33CC00\"][!--/coloro--]__CLASS__[!--colorc--][/span][!--/colorc--] and [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]get_class()[!--colorc--][/span][!--/colorc--], but that only returns the class name, not the variable it is declared under.Thanks for your help!-Matt Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/ Share on other sites More sharing options...
jeremywesselman Posted June 5, 2006 Share Posted June 5, 2006 How do you expect to call $MyNewClassObject->getVarName(); when you are trying to find out what $MyNewClassObject is? Think through your question before you ask.[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--] Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41926 Share on other sites More sharing options...
calcop Posted June 5, 2006 Author Share Posted June 5, 2006 Think though your reply, before you reply.It was just sample code.I "as the scripter" know what the class name is. I need the class to be able to know what variable it is existing in. Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41928 Share on other sites More sharing options...
jeremywesselman Posted June 5, 2006 Share Posted June 5, 2006 Then why don't you just pass it into the constructor when you instantiate it.[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--] Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41929 Share on other sites More sharing options...
calcop Posted June 5, 2006 Author Share Posted June 5, 2006 What I am trying to do is make a class that automates text boxes for forms. Does all the error checking, validation, etc.I need the class to automaticly populate the updated infromation from $_GET or $_POST. So, I need the class to know what its own variable name is, so I could use it for the names of the text boxes. I did think about passing it to the constructor, but that would open a big door to errors (human errors).I decided to make an automated key for each textbox name. So far it is working good. After spending a lot of time resarching, I don't think there is a way that you can have your class lookup which variable it exists in.Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41930 Share on other sites More sharing options...
samshel Posted June 5, 2006 Share Posted June 5, 2006 You are creating Objects dynamically ?...is it a bad idea to use a single object and call its methods repeatedly ? this way u dont need to know the name of the variable...you will have only one object...Also you can try:bool is_a ( object object, string class_name)This function returns TRUE if the object is of this class or has this class as one of its parents, FALSE otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41962 Share on other sites More sharing options...
trq Posted June 5, 2006 Share Posted June 5, 2006 You'd need to use variable variables, but still, I dont see the point. Quote Link to comment https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.