943,910 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 7013
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 30th, 2009
-1

How to list class variables and their values?

Expand Post »
Hi,

Without first knowng what public class variables the class has, how can i display a list of all the variables in that class along with the values?

PHP Syntax (Toggle Plain Text)
  1. print_r(new MyClass());

will display the public variables and values, plus some other unwanted entries, but I cannot do anything with them.

I want to be able to create name=value pairs for an HTML GET request using the class variables.

Thanks for reading this.
Last edited by jakesee; Apr 30th, 2009 at 3:53 pm.
Similar Threads
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 30th, 2009
-1

Re: How to list class variables and their values?

any correctly coded class has a get and set for it's variables
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

Click to Expand / Collapse  Quote originally posted by kireol ...
any correctly coded class has a get and set for it's variables
That's not necessarily the case.

PHP Syntax (Toggle Plain Text)
  1. public function AddVar($name, $val)
  2. {
  3. $this->$name = $val;
  4. }

So do you know how to get the list of variables? Please?
Last edited by jakesee; Apr 30th, 2009 at 4:35 pm.
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

yes, that is the case.


Any class that is coded correctly has get and set for all class variables that need to be get and set. that's OO at it's basic core.

http://en.wikipedia.org/wiki/Encapsu...mputer_science)
Last edited by kireol; Apr 30th, 2009 at 5:12 pm. Reason: added link for you
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

Click to Expand / Collapse  Quote originally posted by kireol ...
yes, that is the case.

Any class that is coded correctly has get and set for all class variables that need to be get and set. that's OO at it's basic core.

http://en.wikipedia.org/wiki/Encapsu...mputer_science)
Wikipedia is not a bible and standards change. I would appreciate if you can answer the question. If you do no know, its ok. thanks for the input.
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

Click to Expand / Collapse  Quote originally posted by jakesee ...
Wikipedia is not a bible and standards change. I would appreciate if you can answer the question. If you do no know, its ok. thanks for the input.
Maybe you are new to OO programming; everyone must start somewhere. I'm sorry that you don't understand beginner level concepts. It looks like you just want to argue with facts and the way things truly are.


Good luck with whatever it is you are doing.
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

I do not want to argue with you, but in case you think I am sneaking around, I would like to inform you that I have to give you a negative reputation now because you are not answering the question.

It has to be "this is how it is done" or "it cannot be done" instead of arguing whether it is "correctly coded" or "good programming practice".

EDIT: After posting this, I went on into google to do a search "list class variables" and this post actually came out first on the list! You see, by not answering the question (any question at all), you are wasting not just your time and my time, any one else who comes along to this post will be wasted. I hope you do understand me.
Last edited by jakesee; Apr 30th, 2009 at 5:55 pm.
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 30th, 2009
0

Re: How to list class variables and their values?

wow, that's a lot of ranting without actually answering his question.
php Syntax (Toggle Plain Text)
  1. $vars = get_class_vars('Class');
  2. // or
  3. $vars = get_class_vars(new Class());
There are more in-depth methods (see Reflection in the PHP manual) but this will get the job done
Last edited by ShawnCplus; Apr 30th, 2009 at 5:57 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Apr 30th, 2009
0

Re: How to list OBJECT variables and their values?

finally! I actually given up on this thread because I thought no one would come inside a thread so many replies.

thanks for the insight ShawnCPlus. Unfortunately, that function doesn't do what I want. because it does not accept an object. maybe I phrased my question wrongly.

$object = new MyClass();
// after some funny operations
// that dynamically creates more member variables
$var = get_class_variables($object); //error.

I'll look up "Reflection" on PHP first. thanks!!
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 30th, 2009
0

Re: How to list OBJECT variables and their values?

Click to Expand / Collapse  Quote originally posted by jakesee ...
finally! I actually given up on this thread because I thought no one would come inside a thread so many replies.

thanks for the insight ShawnCPlus. Unfortunately, that function doesn't do what I want. because it does not accept an object. maybe I phrased my question wrongly.

$object = new MyClass();
// after some funny operations
// that dynamically creates more member variables
$var = get_class_variables($object); //error.

I'll look up "Reflection" on PHP first. thanks!!
it's actually get_class_vars, not get_class_variables.
If that still doesn't work try
php Syntax (Toggle Plain Text)
  1. $var = get_class_vars(get_class($object));
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: working out savings for a site
Next Thread in PHP Forum Timeline: Can anyone help me achieve this...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC