Comparing two objects in php

January 24, 2013
$areSame = (serialize($ob1) === serialize($ob2));

This easy way of comparing two objects in php.It will take in account object variables.
Example :

class ob1{
	public $number = 1;
}
class ob2{
	public $number = 1;
}

$areSame = serialize(new ob1()) === serialize(new ob2())
//$areSame = true;

class ob1{
	public $number = 1;
}
class ob2{
	public $number = 2;
}

$areSame = serialize(new ob1()) === serialize(new ob2())
//$areSame = false;

 

Suggestions or problems ?

Write a comment.

tags: ,
posted in OOP, php by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org