Home  >  Article  >  Backend Development  >  Detailed explanation of object comparison usage in PHP object-oriented

Detailed explanation of object comparison usage in PHP object-oriented

巴扎黑
巴扎黑Original
2017-04-17 14:38:491638browse

Through cloning, we can understand the meaning of $a = $b and $a = clone $b. But in practical applications, we still need to determine whether the relationship between two objects is a clone or a reference. This can be done by using the comparison operators "==" and "===".

Operators "==" and "==="

When using comparison Operator (==) Compare two object variables When, the comparison principle is: If the attributes and attribute values ​​​​of the two objects are equal, and the two objects are instances of the same class, then the two object variables are equal;

And if you use the congruent operator (===), these two object variables must point to the same instance of a certain type (that is, the same object).

Let’s look at an example below:

<?php
header("content-type:text/html;charset=utf-8");
class Dog{
public $type;
public $age;
function __construct($type,$age)
{
$this->type = $type;
$this->age = $age;
}
}
$dog1 = new Dog(&#39;二哈&#39;,&#39;2&#39;);
$dog2 = new Dog(&#39;二哈&#39;,&#39;2&#39;);
if($dog1 == $dog2){
echo &#39;<br/> $dog1 == $dog2&#39;;
}
if($dog1 === $dog2){
echo &#39;<br/>$dog1 === $dog2 &#39;;
}else{
echo &#39;<br/>他们不能全等&#39;;
}
echo &#39;<hr/>&#39;;
$dog3 = $dog1;
if($dog1 == $dog3){
echo &#39;<br/> $dog1 == $dog3&#39;;
}
if($dog1 === $dog3){
echo &#39;<br/>$dog1 === $dog3 &#39;;
}else{
echo &#39;<br/>他们不能全等&#39;;
}

Example analysis:

First we create a dog class and define the attribute type and age in the class , create a constructor. Then instantiate two identical classes, $dog1 and $dog2. Then compare the two instantiated classes. First use the comparison operator "==". We mentioned above the meaning of this symbol: when using the comparison operator (==) to compare two object variables, the comparison principle is: if the attributes and attribute values ​​​​of the two objects are equal, and the two objects are the same instance of the class, then the two object variables are equal. First of all, it is judged that the attributes and attribute values ​​of $dog1 and $dog2 are equal, and secondly, they are both instantiation results of the same Dog-like class, then the result is, $dog1==$dog2. But is the next judgment $dog1===$dog2 equal? Let's take a look at the conditions for the operator "===" to hold: And if you use the congruence operator (===), the two object variables must point to the same instance of a certain type (that is, the same object). This can only be true when the values ​​compared on both sides of the operator "===" are the same object. $dog1 and $dog2 are two instances, not the same, so their "===" cannot be established. We give a condition, $dog3 = $dog1, and use the same method to compare and judge objects to see if it is true.

You can run the above example to see the results.

The above is the detailed content of Detailed explanation of object comparison usage in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn