954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to list class variables and their values?

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?

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.

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

any correctly coded class has a get and set for it's variables

kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 
any correctly coded class has a get and set for it's variables

That's not necessarily the case.

public function AddVar($name, $val)
{
        $this->$name = $val;
}


So do you know how to get the list of variables? Please?

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

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/Encapsulation_(computer_science )

kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

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/Encapsulation_(computer_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.

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 
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.

kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

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.

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

wow, that's a lot of ranting without actually answering his question.

$vars = get_class_vars('Class');
// or
$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

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

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

$var = get_class_vars(get_class($object));
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

yah. not variables, it's vars. my typo error in the post.

no errors but not doing what I want. I have read the magic methods and all seems like none of them do what i want. so far, var_dump, var_export and print_r is closest.

class MyClass
{
      private static a = "some value";
      
      function addVar($name, $val)
     {
                $this->$name = $val;
                // according to var_export,
                // this seems to be implicit
                // __set_state()
      }
     
      function LoadVariables()
     {
            // works but not what i want
            // this gives the static var
            get_class_vars(get_class($this));

            // this gives all the vars added using
            // the addVar and neatly, without the static var
            print_r($this);   // this is what i want.
            // but print_r gives a string... that's bad.
            // so I do some string manipulation and an
            // eval() to make a new array
            // but i think there might be just one function to do this.
     }
}
jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

If you're doing a container of sorts where you dynamically add variables and get them you might want something like this.

class Container
{
  private $holder;
  public function __get($key)
  {
    return isset($this->holder[$key]) ? $this->holder[$key] : false;
  }

  public function __set($key, $value)
  {
    $this->holder[$key] = $value;
  }

  public function toArray()
  {
    return print_r($this->holder, true);
  }
}
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Oh.... so that works... ok, i learnt something new today. thanks!

but for me to use that will mean breaking up old code and base classes... =(

I think I can proceed from here now. thanks alot for your help.

jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
 

Oh.... so that works... ok, i learnt something new today. thanks!

but for me to use that will mean breaking up old code and base classes... =(

I think I can proceed from here now. thanks alot for your help.

Well you don't have to use that. The basic concept to learn is that when using dynamic variables with __get and __set its usually better practice to use a holder variable so you have better separation of class variables and dynamic variables.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

btw,

get_object_vars($object);

is actually what he was looking for.

tonymeman
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

... even tho i'm with kireol, it's not good programming practice. You should store dynamic properties in an array, and make use of PHP's __set() and __get() methods.

tonymeman
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

You can try

while (list($key,$value) = each($object)) {
	echo $key.":".$value;
}


$object is an object or an array.

Setvir
Newbie Poster
22 posts since Nov 2010
Reputation Points: 12
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You