获取用户在类外定义的所有变量的列表

i have something like this:

class foo
{
   //code
}

$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable";  //create foo->otherVariable

i can get in class foo a list of all variables defined outside by user (newVariable, otherVariable,etc)? Like this:

class foo
{
   public function getUserDefined()
   {
      // code

   }
}

$var = new foo();
$var->newVariable = 1; // create foo->newVariable
$var->otherVariable = "hello, im a variable";  //create foo->otherVariable
var_dump($var->getUserDefined()); // returns array ("newVariable","otherVariable");

Thanks!.

Yes, using get_object_vars() and get_class_vars():

class A {
var $hello = 'world';
}
$a = new A();
$a->another = 'variable';
echo var_dump(get_object_vars($a));
echo '<hr />';
// Then, you can strip off default properties using get_class_vars('A');
$b = get_object_vars($a);
$c = get_class_vars('A');
foreach ($b as $key => $value) {
    if (!array_key_exists($key,$c)) echo $key . ' => ' . $value . '<br />';
}

You question is not clear though.

$var->newVariable = 1;

there are two possible contex of above expression

1) you are accessing class public variables.

like

class foo
{
  public $foo;
  public function method()
  {
     //code
   }
}
 $obj_foo = new foo();
 $obj_foo->foo = 'class variable';

OR

2) you are defining class variable runtime using _get and _set

class foo
{
  public $foo;
  public $array = array();
  public function method()
  {
     //code
  }
  public function __get()
  {
    //some code
  }
  public function __set()
  {
    // some code
  }


}
 $obj_foo = new foo();
 $obj_foo->bar= 'define class variable outside the class';

so in which context your question is talking about?

What is your goal? Imo it's not very good practice (unless you really know what you are doing). Maybe it's good idea consider create some class property like "$parameters" and then create setter and getter for this and use it in this way:

class foo {
    private $variables;

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

    public function getVariable($key) {
        return $this->variables[$key];
    }

    public function hasVariable($key) {
        return isset($this->variables[$key]);
    }

    (...)
 }

$var = new foo();

$var->addVariable('newVariable', 1); 
$var->addVariable('otherVariable', "hello, im a variable"); 

And then you can use it whatever you want, for example get defined variable:

$var->getVariable('otherVariable');

To check if some var is already defined:

$var->hasVariable('someVariable')