Jump to content

Archived

This topic is now archived and is closed to further replies.

calcop

Class Name

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

Share this post


Link to post
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--]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
You'd need to use variable variables, but still, I dont see the point.

Share this post


Link to post
Share on other sites

×

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.