您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 吐鲁番分类信息网,免费分类信息发布

PHP比较对象

2024/3/4 19:23:32发布12次查看
简介php 有一个比较运算符 ==,使用它可以执行两个 objecs 变量的简单比较。如果两者属于同一类并且相应属性的值相同,则返回 true。
php 的 === 运算符比较两个对象变量,当且仅当它们引用时返回 true相同类的相同实例
我们使用以下两个类来比较对象与这些操作符
示例<?phpclass 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; }}?>
同一类的两个对象示例$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);
输出two objects of same classusing == operator : bool(true)using === operator : bool(false)
同一对象的两个引用示例$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);
输出two references of same objectusing == operator : bool(true)using === operator : bool(true)
两个不同类的对象示例$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 shows following result
two objects of different classesusing == operator : bool(false)using === operator : bool(false)
以上就是php比较对象的详细内容。
吐鲁番分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录