Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/11201-class-name/
Share on other sites

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--]
Link to comment
https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41926
Share on other sites

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!
Link to comment
https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41930
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/11201-class-name/#findComment-41962
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.