1

I know that I can use this to iterate on an object:

   foreach ($this as $key => $value) {
       print "$key => $value\n";
   }

Is the order in which the properties are looked over always the same or is it random?
I tried it a bit and it seems to always follow the declaration order, as in the exemple from here: http://php.net/manual/en/language.oop5.iterations.php

But I couldn't find any documentation where this is formally specified.
Can an experimented PHP dev confirm or disconfirm what I found in my trials?

aurelienC
  • 1,113
  • 3
  • 12
  • 22
  • it would appear the order is the same as that in which the properties are intially defined. – Professor Abronsius Aug 19 '16 at 15:21
  • 2
    This looks related: http://stackoverflow.com/questions/4733188/is-foreach-guaranteed-to-iterate-in-the-array-order-in-php – Henders Aug 19 '16 at 15:26
  • 3
    In [this rather extensive answer about foreach](http://stackoverflow.com/a/14854568/2734189), a PHP core developer confirms that object iteration is ordered. – Don't Panic Aug 19 '16 at 15:30

0 Answers0