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.

Comments
negative rep for negative repping me for telling the truth

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

Comments
Not answering the question: it's got to be "this is how it is done" or "it cannot be done"

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?

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.

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.

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.

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

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

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));

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.
     }
}

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);
  }
}
Comments
Very helpful

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.

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.

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

You can try

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

$object is an object or an array.

Edited 6 Years Ago by Setvir: Fixed code tag.

This question has already been answered. Start a new discussion instead.