Jump to content

Does the order of an object's properties mean anything?


Go to solution Solved by requinix,

Recommended Posts



<?php


function bool2str($bool)
{
    if ($bool === false) {
        return 'FALSE';
    } else {
        return 'TRUE';
    }
}


function compareObjects(&$o1, &$o2)
{
    echo '$o1'."\n";
    var_dump($o1);
    echo '$o2'."\n";
    var_dump($o2);
    echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
    echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n";
    echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n";
    echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n\n\n";
}




$o1 = new \stdClass;
$o1->a=123;
$o1->b=222;
$o1->c=321;


$o2 = new \stdClass;
$o2->c=321;
$o2->b=222;
$o2->a=123;


$o3 = new \stdClass;
$o3->a=123;
$o3->b=222;
$o3->c=321;


compareObjects($o1, $o1);
compareObjects($o1, $o2);
compareObjects($o1, $o3);



$o1
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  int(222)
  ["c"]=>
  int(321)
}
$o2
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  int(222)
  ["c"]=>
  int(321)
}
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE




$o1
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  int(222)
  ["c"]=>
  int(321)
}
$o2
object(stdClass)#2 (3) {
  ["c"]=>
  int(321)
  ["b"]=>
  int(222)
  ["a"]=>
  int(123)
}
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE




$o1
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  int(222)
  ["c"]=>
  int(321)
}
$o2
object(stdClass)#3 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  int(222)
  ["c"]=>
  int(321)
}
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE




Order does not matter. Loose object comparison works like an array comparison.

 

Guess I kind of knew that as my little script proved it.  Still thought "maybe" there was some scenario where it might.  Thanks for the confirmation.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.