>  기사  >  백엔드 개발  >  PHP 비교 객체

PHP 비교 객체

WBOY
WBOY앞으로
2023-08-30 15:29:061292검색

PHP 비교 객체

소개

PHP에는 두 개체 변수의 간단한 비교를 수행하는 데 사용할 수 있는 비교 연산자==가 있습니다. 둘 다 동일한 클래스에 속하고 해당 속성의 값이 동일한 경우 true를 반환합니다.

PHP의 === 연산자는 두 개체 변수를 비교하고 동일한 클래스의 동일한 인스턴스를 참조하는 경우에만 true를 반환합니다.

다음 두 클래스를 사용하여 개체를 이러한 연산자와 비교합니다.

<?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;
   }
}
?>

동일한 클래스의 두 객체

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)

동일한 객체에 대한 두 개의 참조

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)

다른 클래스의 두 객체

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 다음 결과를 보여줍니다

rreee

위 내용은 PHP 비교 객체의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제