1

I'm trying to compare two objects like this:

    if($obj1==$obj2){
    ...

But my comparison always return false!

$obj1 is a param of my function (called through a Webservice) of type "MySoapObject".

$obj2 is an object of type "MySoapObject" loaded from my database.

In my test print_r of my both objects give me this:

stdClass Object
(
    [establishment] => stdClass Object
        (
            [f_establishment_id] => 4
            [f_name] => MyEstablishment
            [f_display_name] => v
            [f_main_web_link] => 
            [f_contact] => 
            [f_country_code] => 
            [f_city] => 
            [f_zip_code] => 
            [f_postal_address] => 
            [f_default_language] => 
            [f_timezone] => 
            [f_cloud_synchronisation_action] => TO_SYNC
        )

    [site_list] => Array
        (
            [0] => stdClass Object
                (
                    [f_site_id] => Site1
                    [f_status] => TO_DISABLE
                    [f_name] => Site1
                )

        )

)

By the way, if I try this:

    if(print_r($obj1)==print_r($obj2)){
    ...

The result is true!

Have you an idea of where could be the problem? How to solve it? Thanks for your help.

cyanat
  • 77
  • 7
  • Check this https://stackoverflow.com/questions/20531967/comparing-2-objects-php – Claudio Dec 15 '17 at 17:41
  • 1
    FYI, the reason `print_r($obj1)==print_r($obj2)` is `TRUE` is because `print_r()` returns `TRUE` unless you pass the flag that tells it to return a string. So you're essentially comparing `TRUE==TRUE`, which, obviously, is `TRUE`. – Patrick Q Dec 15 '17 at 17:42
  • Did you read this http://php.net/manual/en/language.oop5.object-comparison.php ? Check, are both your objects really instances of the same class or not. BTW: I'm pretty sure that using object comparison for case with nested objects is not a good idea because of recursion. – Victor Perov Dec 15 '17 at 17:43
  • BTW: if you think about comparison like strings - you have to think about sorting all your keys right before converting object to a string. You can do convert object to string, as example, by **json_encode**. – Victor Perov Dec 15 '17 at 17:52
  • Indeed, print_r($obj1)==print_r($obj2) will always be true. Nevertheless I can see that the two string returned are exactly the same. Of course I can create a function to check property by property, but I would have prefer to avoid that. – cyanat Dec 16 '17 at 17:27

1 Answers1

-1

for object comparison you can use :

 if($obj1===$obj2){
 }

check the documentation http://php.net/manual/en/language.operators.comparison.php

Mandeep Singh
  • 55
  • 1
  • 8