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

 
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org