Jump to content


Photo

Class Name


  • Please log in to reply
6 replies to this topic

#1 calcop

calcop
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 05 June 2006 - 12:17 AM

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:

$MyNewClassObject = new SideBar;
$MyNewClassObject->getVarName ();

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

#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 05 June 2006 - 02:38 AM

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--]

#3 calcop

calcop
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 05 June 2006 - 02:46 AM

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.

#4 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 05 June 2006 - 02:48 AM

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--]

#5 calcop

calcop
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 05 June 2006 - 02:54 AM

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!

#6 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 05 June 2006 - 06:47 AM

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.

Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 June 2006 - 07:13 AM

You'd need to use variable variables, but still, I dont see the point.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users