This may not be exactly what you might have expected, because what you are expecting is not available as a PHP language function (unfortunately), but here comes a well-known workaround:
Use get_object_vars() for this:
$f = new Foo(); var_dump(get_object_vars($f));
It will return an associative array with property names as indices and their values. Check out this example:
class Foo { public $bar = 'hello world'; // even protected and private members will get exported: protected $test = 'I\'m protected'; private $test2 = 'I\'m private'; public function toArray() { return get_object_vars($this); } } $f = new Foo(); var_dump($f->toArray());
Output:
array(2) { 'bar' => string(11) "hello world" 'test' => string(13) "I'm protected" 'test2' => string(13) "I'm private" }
source share