116

How can I loop through all the properties of object?. Right now I have to write a new code line to print each property of object

echo $obj->name;
echo $obj->age;

Can I loop through all the properties of an object using foreach loop or any loop?

Something like this

foreach ($obj as $property => $value)  
4
  • 3
    Go head with foreach loop you are doing right Feb 12, 2011 at 6:09
  • 7
    Use a foreach (with => not ->) to iterate through the object public properties. You can also make your class Traversable if you want another behaviour.
    – netcoder
    Feb 12, 2011 at 6:29
  • @netcoder: thanks but that was a typo only
    – Daric
    Feb 12, 2011 at 6:31
  • 2
    as netcoder said: only public properties will be accessible
    – Andreas
    Feb 12, 2011 at 8:21

7 Answers 7

177

If this is just for debugging output, you can use the following to see all the types and values as well.

var_dump($obj);

If you want more control over the output you can use this:

foreach ($obj as $key => $value) {
    echo "$key => $value\n";
}
2
  • 12
    "Object of class stdClass could not be converted to string" could well be a resulting error if the object is not an array.
    – landed
    Jul 22, 2016 at 12:03
  • print_r($obj); can also help. Prints given object's fields recursively.
    – fandasson
    Aug 4, 2017 at 18:48
14

For testing purposes I use the following:

//return assoc array when called from outside the class it will only contain public properties and values 
var_dump(get_object_vars($obj)); 
1
  • If the class of $obj is written/controlled by once own, there is also __debugInfo() magic method to control what var_dump() shows - very handy! Otherwise this answer reduces verbosity, which can also be useful in a step debugger session for E_TOO_MUCH_INFORMATION.
    – hakre
    Aug 12, 2022 at 23:40
9

Before you run the $obj through a foreach loop you have to convert it to an array (see: cast to array) if you're looking for properties regardless of visibility.

Example with HTML output (PHP 8.1):

foreach ((array)$obj as $key => $val) {
    printf(
        "%s: %s<br>\n", 
        htmlspecialchars("$key"), 
        htmlspecialchars("$val"),
    );
}
3

Sometimes, you need to list the variables of an object and not for debugging purposes. The right way to do it is using get_object_vars($obj). It returns an array that has all the visible class variables and their value. You can then loop through them in a foreach-loop. If used within the object itself, simply do get_object_vars($this).

3

Using get_object_vars is better than applying a foreach directly to the object since your code will be also compliant with PHPStan which requires that foreach is applied only to iterables (see https://github.com/phpstan/phpstan/issues/1060).

Thus, the best choice is go like this:

foreach (get_object_vars($obj) as $key => $value) {
    echo "$key => $value\n";
}
1

Here is another way to express the object property.

foreach ($obj as $key=>$value) {
    echo "$key => $obj[$key]\n";
}
2
  • 5
    This only works if the object implements \ArrayAccess or is an array, otherwise the following is thrown: FATAL ERROR Uncaught Error: Cannot use object of type SomeType as array Nov 2, 2018 at 16:46
  • You already have the value so just do echo "$key => $value\n";
    – Jacob
    Aug 30, 2022 at 20:59
0

David's answer is solid as long as either: a) you only need access to public attributes, or b) you are working with the stdClass object. If you have defined private or protected attributes in a class and want to display all attributes, simply add an instance method that iterates the properties:

class MyClass {

  public $public_attr1;
  private $private_attr2;
  protected $protected_attr3;

  function iterateAttributes() {
    foreach ($this as $attr=>$value) {
      echo "$attr: $value <br/>";
    }
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.