get_object_vars() has confusing behaviour when called as get_object_vars($this) or similar: since a method inside a class definition can access private vars, apparently so can get_object_vars(), so it returns private vars as well.

A simple workaround is to define a method that in itself calls get_object_vars, like:

actually, it's not entirely true that php5 will only return public members....php5 will return any variable IT HAS ACCESS TO

In other words, if you do a get_class_variables($this) inside a class, you'll get everything - public, private, the whole shebang...really annoying since you can't check to see what's private/public without using reflection

It seems that get_object_vars will now return properties of an object even if they have no value - meaning only defined by var $foo in the class declaration. This is noted behaviour in 4.2.1 which is different from previous versions and hitherto undocumented on this page.

So, to iterate over the properties of an ArrayObject, and not the array values:

<?php

$obj = new Toot(); // which is a subclass of ArrayObject$props = get_class_vars(get_class($obj));foreach($props as $prop => $defaultValue) {$value = $obj->$prop; // now you have the property name and its value}

If your object contains a reference, beware that you might get references for all object properties in the array values, thus when changing values in the array, they may change the object too (confirmed and not yet fixed PHP bug https://bugs.php.net/bug.php?id=66961 affecting all PHP versions up to at least 5.6.2 and 5.5.18 and 5.4.34).

/** * Just a example of a child class using the functionality * * Note: if you don't wanna to allow the change of some attribute * by this method you can just make more complex the __setAttribute function. * * @author Renan de Lima ( renandelima@gmail.com ) * @author Thiago Mata ( thiago.henrique.mata@gmail.com ) * @date 2007-02-21 */class son extends father{

// Printing the object using print_r() shows the changes// have been done on the object.print("Object Changed (print_r):\n");print_r($test);print("\n");

// If you dont now the class propertynames you can get these by casting the// object to an array. The array keys are the names$prop = array_keys((array) $test);print("Getting all class propertynames (print_r)\n");print_r($prop);

if you are trying to deep-copy an object with get_object_vars(), strange behaviour can accidentally clobber your original object properties. please read very, very carefully:

get_object_vars() may either return references to *or* deep copies of the object's properties *depending on whether that property has been set with the -> operator*. (this behaviour probably varies per php platform and os so please confirm for yourself.)

furthermore, consider $properties = get_object_vars($obj);normally, unset()ting a reference does not affect the original, i.e. $ref = NULL; is not the same as unset($ref); per the references documentation. However, if you have this strange references version and you unset() an array element of $properties, it will *SET THE OBJECT PROPERTY TO NULL*, which is not how references normally work.

even stranger behaviour comes into effect that i can only express with an example. please test this with your version and OS and proceed very carefully:--><?phpecho "<pre>\n";

[NOTE: You must pass in a reference to an object, not an object. Sorry if this offends PHP'ers, but the distinction of pass-by-value and copy-on-assignment drives me batty (compared to Python, Java, Smalltalk), so I make all my functions pass by value, and force myself to pass in a reference to keep track of what is happening under the hood.]

Be aware of the fact that this is scope-sensitive. If you're calling this from an objects own method, then private and protected vars will be outputted as well. Call it from outside the object and the result will most likely be what you want to archive.

if I call<?$single_object = $data_array_of_objects[0];$array_of_objectvars = get_object_vars($single_object); foreach($array_of_objectvars as $key => $val) { echo(" $key => $val<br>"); }?>I get only _ONE_ line with the $key = first variable name of the object and $val = the values of _ALL_ variables of the object including the first separated by a space.

I get a list of $key = $ val as expected, before the other echos' are printed.It seems to me that get_object_vars works differently when you access a variable in those objects explicitly (as in the echos)

This is a slight modification of the previous poster's function. We ran into a problem using this function when we had a JS array nested inside a JS hash.

Something like this...

myHash = new Hash();myHash[address] = new Array();

When we threw that at this function, it found the first hash as an object, and then using the previous poster's function, it did not consider the array as an "object." Instead it gave us a std_object type and we were unable to make any use of it.

The modification we made was adding a check for is_array inside both the is_object and is_array checks when you call the function. This checks for an array inside either an object or a nested array.

Since there's no apparent means of obtaining all the *private* properties in an object I wrote a little function to do it. Built in support would be much more efficient since mine uses a preg_ search to do this....

So if you run it with the optional second paramter missing you'll just get an array of the variable names that are private inside the class. This is the only option if you are not inside the actual object and the object has no private properties inherited.

If you run it with the second parameter set to true you will get an associative array with the properties and their corresponding values. I'd only advise to do that for singletons since you may get errors if there are any private properites in parents/children.