Hướng dẫn comparing objects in php - so sánh các đối tượng trong php

Khi sử dụng toán tử so sánh (==), các biến đối tượng được so sánh theo cách đơn giản, cụ thể là: hai trường hợp đối tượng bằng nhau nếu chúng có cùng thuộc tính và giá trị (giá trị được so sánh với ==) và là các trường hợp của cùng một lớp.

Khi sử dụng toán tử nhận dạng (===), các biến đối tượng là giống hệt nhau khi và chỉ khi chúng đề cập đến cùng một thể hiện của cùng một lớp.

Một ví dụ sẽ làm rõ các quy tắc này.

Ví dụ #1 Ví dụ về so sánh đối tượng

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}

function

compareObjects(&$o1, &$o2)
{
    echo 
'o1 == o2 : ' bool2str($o1 == $o2) . "\n";
    echo 
'o1 != o2 : ' bool2str($o1 != $o2) . "\n";
    echo 
'o1 === o2 : ' bool2str($o1 === $o2) . "\n";
    echo 
'o1 !== o2 : ' bool2str($o1 !== $o2) . "\n";
}

class

Flag
{
    public 
$flag;

    function

__construct($flag true) {
        
$this->flag $flag;
    }
}

class

OtherFlag
{
    public 
$flag;

    function

__construct($flag true) {
        
$this->flag $flag;
    }
}
$o = new Flag();
$p = new Flag();
$q $o;
$r = new OtherFlag();

echo

"Two instances of the same class\n";
compareObjects($o$p);

echo

"\nTwo references to the same instance\n";
compareObjects($o$q);

echo

"\nInstances of two different classes\n";
compareObjects($o$r);
?>

Ví dụ trên sẽ xuất ra:

Two instances of the same class
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Two references to the same instance
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Instances of two different classes
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Ghi chú::

Các tiện ích mở rộng có thể xác định các quy tắc riêng để so sánh đối tượng của họ (==).

jazfresh tại hotmail.com ¶

15 năm trước

Note that when comparing object attributes, the comparison is recursive (at least, it is with PHP 5.2). That is, if $a->x contains an object then that will be compared with $b->x in the same manner. Be aware that this can lead to recursion errors:
class Foo {
    public
$x;
}
$a = new Foo();
$b = new Foo();
$a->x = $b;
$b->x = $a;print_r($a == $b);
?>
Results in:
PHP Fatal error:  Nesting level too deep - recursive dependency? in test.php on line 11

Ẩn danh ¶

12 năm trước

Comparison using <> operators should be documented.  Between two objects, at least in PHP5.3, the comparison operation stops and returns at the first unequal property found.

==0

==1

==2

rnealxp tại yahoo dot com ¶

2 năm trước

==3

rnealxp tại yahoo dot com ¶

2 năm trước

==4

==5

==6

==7

5 năm trước

Nhuhoai

==8

==9

==0

==1

8 năm trước

WBCarts tại Juno Dot Com ¶

==2

==3

==4

==5

==6

14 năm trước

15 năm trước

==7

==8

==1

Ẩn danh ¶

WBCarts tại Juno Dot Com ¶

===0

===1

===2

==1

14 năm trước

15 năm trước

===4

===5

===6

===7

===8

===9

==1

Ẩn danh ¶

WBCarts tại Juno Dot Com ¶

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
1

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
2

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
3

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
4

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
5

14 năm trước

Rune tại Zedeler Dot DK ¶

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
6

function bool2str($bool)
{
    if (
$bool === false) {
        return 
'FALSE';
    } else {
        return 
'TRUE';
    }
}
7

==1