<p>In effect, if I have a <code>class c</code> and instances of <code>$c1</code> and <code>$c2</code> 
which might have different private variable amounts but all their public methods return the same values I would like to be able to check that <code>$c1 == $c2?</code></p>

<p>Does anyone know an easy way to do this?</p>

[{'answer_id': 55731, 'author': 'Rushi', 'author_id': 3983, 'author_profile': 'https://Stackoverflow.com/users/3983', 'pm_score': 1, 'selected': False, 'text': '<p>You can define PHP\'s <strong>__toString</strong> magic method inside your class. </p>\n\n<p>For example</p>\n\n<pre><code>class cat {\n    private $name;\n    public function __contruct($catname) {\n        $this-&gt;name = $catname;\n    }\n\n    public function __toString() {\n        return "My name is " . $this-&gt;name . "\\n";\n    }\n}\n\n$max = new cat(\'max\');\n$toby = new cat(\'toby\');\nprint $max; // echoes \'My name is max\'\nprint $toby; // echoes \'My name is toby\'\n\nif($max == $toby) {\n    echo \'Woohoo!\\n\';\n} else {\n    echo \'Doh!\\n\';\n}\n</code></pre>\n\n<p>Then you can use the equality operator to check if both instances are equal or not. </p>\n\n<p>HTH,</p>\n\n<p>Rushi</p>\n'}, {'answer_id': 55742, 'author': 'Rushi', 'author_id': 3983, 'author_profile': 'https://Stackoverflow.com/users/3983', 'pm_score': 1, 'selected': False, 'text': '<p>George: You may have already seen this but it may help: <a href="http://usphp.com/manual/en/language.oop5.object-comparison.php" rel="nofollow noreferrer">http://usphp.com/manual/en/language.oop5.object-comparison.php</a></p>\n\n<blockquote>\n  <p>When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class. </p>\n</blockquote>\n\n<p>They don\'t get implicitly converted to strings.</p>\n\n<p>If you want todo comparison, you will end up modifying your classes. You can also write some method of your own todo comparison using getters &amp; setters</p>\n'}, {'answer_id': 55811, 'author': 'Rushi', 'author_id': 3983, 'author_profile': 'https://Stackoverflow.com/users/3983', 'pm_score': 0, 'selected': False, 'text': '<p>You can try writing a class of your own to plugin and write methods that do comparison based on what you define. For example:</p>\n\n<pre><code>class Validate {\n    public function validateName($c1, $c2) {\n        if($c1-&gt;FirstName == "foo" &amp;&amp; $c2-&gt;LastName == "foo") {\n            return true;\n        } else if (// someother condition) {\n            return // someval;\n        } else {\n            return false;\n        }\n    }\n\n    public function validatePhoneNumber($c1, $c2) {\n        // some code\n    }\n}\n</code></pre>\n\n<p>This will probably be the only way where you wont have to modify the pre-existing class code</p>\n'}, {'answer_id': 56012, 'author': 'reefnet_alex', 'author_id': 2745, 'author_profile': 'https://Stackoverflow.com/users/2745', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s difficult to follow exactly what you\'re after. Your question seems to imply that these public methods don\'t require arguments, or that if they did they would be the same arguments. </p>\n\n<p>You could probably get quite far using the inbuilt reflection classes. </p>\n\n<p>Pasted below is a quick test I knocked up to compare the returns of all the public methods of two classes and ensure they were they same. You could easily modify it to ignore non matching public methods (i.e. only check for equality on public methods in class2 which exist in class1). Giving a set of arguments to pass in would be trickier - but could be done with an array of methods names / arguments to call against each class. </p>\n\n<p>Anyway, this may have some bits in it which could be of use to you.</p>\n\n<pre><code>$class1 = new Class1();\n$class2 = new Class2();\n$class3 = new Class3();\n$class4 = new Class4();\n$class5 = new Class5();\n\necho ClassChecker::samePublicMethods($class1,$class2); //should be true\necho ClassChecker::samePublicMethods($class1,$class3); //should be false - different values\necho ClassChecker::samePublicMethods($class1,$class4); //should be false -- class3 contains extra public methods\necho ClassChecker::samePublicMethods($class1,$class5); //should be true -- class5 contains extra private methods\n\nclass ClassChecker {\n\n    public static function samePublicMethods($class1, $class2) {\n\n        $class1methods = array();\n\n        $r = new ReflectionClass($class1);\n        $methods = $r-&gt;getMethods();\n\n        foreach($methods as $m) {\n            if ($m-&gt;isPublic()) {\n                @$result = call_user_method($m-&gt;getName(), $class1);\n                $class1methods[$m-&gt;getName()] = $result;\n            }\n        }\n\n        $r = new ReflectionClass($class2);\n        $methods = $r-&gt;getMethods();\n\n        foreach($methods as $m) {\n\n            //only comparing public methods\n            if ($m-&gt;isPublic()) {\n\n                //public method doesn\'t match method in class1 so return false\n                if(!isset($class1methods[$m-&gt;getName()])) {\n                    return false;\n                }\n\n                //public method of same name doesn\'t return same value so return false\n                @$result = call_user_method($m-&gt;getName(), $class2);\n                if ($class1methods[$m-&gt;getName()] !== $result) {\n                    return false;\n                }\n            }\n        }\n\n        return true;\n    }\n}\n\n\nclass Class1 {\n\n    private $b = \'bbb\';\n\n    public function one() {\n        return 999;\n    }\n\n    public function two() {\n        return "bendy";\n    }\n\n\n}\n\nclass Class2 {\n\n    private $a = \'aaa\';\n\n    public function one() {\n        return 999;\n    }\n\n    public function two() {\n        return "bendy";\n    }\n}\n\nclass Class3 {\n\n    private $c = \'ccc\';\n\n    public function one() {\n        return 222;\n    }\n\n    public function two() {\n        return "bendy";\n    }\n\n\n}\n\nclass Class4 {\n\n    public function one() {\n        return 999;\n    }\n\n    public function two() {\n        return "bendy";\n    }\n\n    public function three() {\n        return true;\n    }\n\n}\n\nclass Class5 {\n\n    public function one() {\n        return 999;\n    }\n\n    public function two() {\n        return "bendy";\n    }\n\n    private function three() {\n        return true;\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 56054, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 3, 'selected': True, 'text': '<p>You can also implement a equal($other) function like</p>\n\n<pre>\n&lt;?php\nclass Foo {\n    public function equals($o) {\n        return ($o instanceof \'Foo\') && $o.firstName()==$this.firstName();\n    }\n}\n</pre>\n\n<p>or use foreach to iterate over the public properties (this behaviour might be overwritten) of one object and compare them to the other object\'s properties.</p>\n\n<pre>\n&lt;?php\nfunction equalsInSomeWay($a, $b) {\n    if ( !($b instanceof $a) ) {\n        return false;\n    }\n\n    foreach($a as $name=>$value) {\n        if ( !isset($b->$name) || $b->$name!=$value ) {\n            return false;\n        }\n    }\n    return true;\n}\n</pre>\n\n<p>(untested)</p>\n\n<p>or (more or less) the same using the Reflection classes, see <a href="http://php.net/manual/en/language.oop5.reflection.php#language.oop5.reflection.reflectionobject" rel="nofollow noreferrer">http://php.net/manual/en/language.oop5.reflection.php#language.oop5.reflection.reflectionobject</a><br />\nWith reflection you might also implement a more duck-typing kind of comparision, if you want to, like "I don\'t care if it\'s an instance of or the same class as long as it has the same public methods and they return the \'same\' values"<br />\nit really depends on how you define "equal".</p>\n'}]
['https://Stackoverflow.com/questions/55713', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5056/']