- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Comparing Objects
Introduction
PHP has a comparison operator == using which a simple comarison of two objecs variables can be performed. It returns true if both belong to same class and values of corresponding properties are are same.
PHP's === operator compares two object variables and returns true if and only if they refer to same instance of same class
We use following two classes for comparison of objects with these oprators
Example
<?php class test1{ private $x; private $y; function __construct($arg1, $arg2){ $this->x=$arg1; $this->y=$arg2; } } class test2{ private $x; private $y; function __construct($arg1, $arg2){ $this->x=$arg1; $this->y=$arg2; } } ?>
two objects of same class
Example
$a=new test1(10,20); $b=new test1(10,20); echo "two objects of same class
"; echo "using == operator : "; var_dump($a==$b); echo "using === operator : "; var_dump($a===$b);
Output
two objects of same class using == operator : bool(true) using === operator : bool(false)
two references of same object
Example
$a=new test1(10,20); $c=$a; echo "two references of same object
"; echo "using == operator : "; var_dump($a==$c); echo "using === operator : "; var_dump($a===$c);
Output
two references of same object using == operator : bool(true) using === operator : bool(true)
two objects of different classes
Example
$a=new test1(10,20); $d=new test2(10,20); echo "two objects of different classes
"; echo "using == operator : "; var_dump($a==$d); echo "using === operator : "; var_dump($a===$d);
Output
Output shows following result
two objects of different classes using == operator : bool(false) using === operator : bool(false)
- Related Articles
- Comparing two dates in PHP
- Comparing float value in PHP
- Comparing String objects using Relational Operators in C++
- Grouping array nested value while comparing 2 objects - JavaScript
- PHP Objects.
- PHP Objects and references
- Comparing objects in JavaScript and return array of common keys having common values
- Creating anonymous objects in PHP
- Storing objects in PHP session
- PHP Generators vs Iterator objects
- How to handle the null values while comparing the two series objects using series.eq() method?
- Jasmine.js comparing arrays
- PHP Call methods of objects in array using array_map?
- Comparing dates using C#
- Comparing dates in Python

Advertisements