Comparison of PHP objects

Source: Internet
Author: User

Object Comparison:

Equality: When comparing two object variables using the comparison operator (= =), the principle of comparison is:
1. If the properties and property values of the two objects are equal,
2. And two objects are instances of the same class (instance of), then the two object variables are equal.

Congruent: And if you use the strict equality operator (= = =), the two object variables must point to the same instance of a class (that is, the same object, which means that the object markers are exactly the same).
class sheep{    public  $name;    protected  $food;     public function __construct (string  $name, string  $food)      {         $this->name =  $name;          $this->food =  $food;    }     public function __tostring ()     {         return serialize ($this);    }    public  Function __clone ()     {        echo  ' object is cloned '  . php_eol;    }} $sheep 1 = new sheep (' radiant ',  ' meat ');//PHP  recoverable fatal error:  object of class sheep could not  be converted to stringecho  $sheep 1;/** * * *  Equality: When comparing two object variables with a comparison operator (= =), the principle of comparison is: if the properties and property values of two objects   are equal, *       and two objects are instances of the same class, the two object variables are equal.  * *   congruent: And if you use the congruent operator (= = =), these two object variables must point to the same instance of a class (that is, the same object).  * */$sheep 1 = new sheep (' radiant ',  ' meat '); $sheep 2 = new sheep (' Radiant '),   ' meat '); $sheep 3 = clone  $sheep 1;//Analysis: Three objects are instances of the sheep class, and the properties and values of the objects are exactly the same if  ($sheep 1 ==   $sheep 2 &&  $sheep 2 ==  $sheep 3)  {    echo  ' $ sheep1 ==  $sheep 2 ==  $sheep 3 '  . php_eol;} $sheep 4 =  $sheep A reference to the 1; //object (passed an object identifier) if  ($sheep 4 ===  $sheep 1)  {     echo  ' $sheep 4 ===  $sheep 1 '  . php_eol;}


Comparison of PHP objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.