Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP Manual > Comparing objects





ลองใช้ค้นหาข้อมูล


   
Bookmark.

Comparing objects

In PHP 4, objects are compared in a very simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class. Similar rules are applied when comparing two objects using the identity operator (===).

If we were to execute the code in the example below:

Example #1 Example of object comparison in PHP 4

<?php
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 {
    var 
$flag;

    function 
Flag($flag=true) {
            
$this->flag $flag;
    }
}

class 
SwitchableFlag extends Flag {

    function 
turnOn() {
        
$this->flag true;
    }

    function 
turnOff() {
        
$this->flag false;
    }
}

$o = new Flag();
$p = new Flag(false);
$q = new Flag();

$r = new SwitchableFlag();

echo 
"Compare instances created with the same parameters\n";
compareObjects($o$q);

echo 
"\nCompare instances created with different parameters\n";
compareObjects($o$p);

echo 
"\nCompare an instance of a parent class with one from a subclass\n";
compareObjects($o$r);
?>

The above example will output:

    
Compare instances created with the same parameters
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Compare instances created with different parameters
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Compare an instance of a parent class with one from a subclass
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE
Which is the output we will expect to obtain given the comparison rules above. Only instances with the same values for their attributes and from the same class are considered equal and identical.

Even in the cases where we have object composition, the same comparison rules apply. In the example below we create a container class that stores an associative array of Flag objects.

Example #2 Compound object comparisons in PHP 4

<?php
class FlagSet {
    var 
$set;

    function 
FlagSet($flagArr = array()) {
        
$this->set $flagArr;
    }

    function 
addFlag($name$flag) {
        
$this->set[$name] = $flag;
    }

    function 
removeFlag($name) {
        if (
array_key_exists($name$this->set)) {
            unset(
$this->set[$name]);
        }
    }
}


$u = new FlagSet();
$u->addFlag('flag1'$o);
$u->addFlag('flag2'$p);
$v = new FlagSet(array('flag1'=>$q'flag2'=>$p));
$w = new FlagSet(array('flag1'=>$q));

echo 
"\nComposite objects u(o,p) and v(q,p)\n";
compareObjects($u$v);

echo 
"\nu(o,p) and w(q)\n";
compareObjects($u$w);
?>

The above example will output:

Composite objects u(o,p) and v(q,p)
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

u(o,p) and w(q)
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE



ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่