Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get the name of a classes declaring variable  from within the class / Get the name of the object's variable from within the object

Posted on 2007-08-07
7
Medium Priority
?
194 Views
Last Modified: 2013-12-13
Is it possible, from within a class, specifically the constructor, to get the name of the declaring variable?
AKA:  I want to get the name of the object's  variable.

eg:

$myClass = new Foo();

class Foo
{
     public __construct()
     {
          // making ths next line up, but you will get what I am trying to do.

          echo  $this->declaring_variable_name;  // myClass;
     }
}
0
Comment
Question by:aventure
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:dryliketoast
ID: 19648530
haha interesting problem :)

If it were me i'd just pass it when its declared. I'm interested to see if there is a better method tho, seems like a bit of a lame way of doing it

$myClass = new Foo('myClass');

class Foo
{
     public __construct($varName="Undefined")
     {
          // making ths next line up, but you will get what I am trying to do.
          $this->declaring_variable_name = $varName;
          echo  $this->declaring_variable_name;  // myClass;
     }
}
0
 
LVL 1

Author Comment

by:aventure
ID: 19649156
I had the same idea, but I thought.. there HAS to be a way.. if Javascript has: arguments.callee.name, surely PHP must have SOME way...
0
 
LVL 1

Expert Comment

by:dryliketoast
ID: 19649404
indeed -but come to think of it,  i can't think of a reason why you would ever need that information. what context are you using this in or what are trying to achieve?

it might be the case that there is simply no need for it :S
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:aventure
ID: 19649430
it's really just a bell and a whistle.  I want the variable name to determine a folder name.  It would be "cool" so to speak, but it would still work without it.
0
 
LVL 1

Accepted Solution

by:
dryliketoast earned 1500 total points
ID: 19649490
yea i figured so :)

i have never come across it in my experience mate, im sure i have read that php is not a true object orientated programming language tho - that might be the reason
0
 
LVL 1

Author Comment

by:aventure
ID: 19649549
Ya.. I know it's not a true OO language and that's why it hurts me... hahahahaha... Oh well.. =)
0
 
LVL 1

Expert Comment

by:dryliketoast
ID: 19649571
lol cheers for the points :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question