PHP 对象和引用总结

PHP 中使用 简单变量对象 时的区别:

① 很多数据类型都可以写时复制(copy-on-write),例:

<?php
$a = 'test1';
$b = $a;
$b = 'test2';
echo $a;

输出:test1

$b = $a 是传值的拷贝,对 $b 做任何改动都不影响原值 $a。

 

② 将一个对象赋值给另一个对象时,并没有复制第一个对象的内容,而是通过 引用 将第二个对象指向第一个对象,例:

<?php
class Human {
    public $name;
}

$person1 = new Human();
$person1->name = 'John';

$person2 = $person1;
$person2->name = 'Dee';

echo 'person one is ',$person1->name,' and person two is ',$person2->name;

输出:person one is Dee and person two is Dee

 

可以通过 == 操作符来比较两个对象,看两者是否具有相同的类和属性;

还可以通过 === 来判断两者是否引用同一个原始对象:

<?php
class Human {
    public $name;
}

$person1 = new Human();
$person1->name = 'John';

$person2 = $person1;
$person2->name = 'Dee';

echo 'person one is ',$person1->name,' and person two is ',$person2->name,'.<br />';

if($person1 == $person2) {
    echo 'equiv <br />';
}

if($person1 === $person2){
    echo 'exact same object!';
}

输出:

person one is Dee and person two is Dee.
equiv 
exact same object!

 

说明:当两个变量指向相同的值时,=== 比较操作符会返回 true。如果两个对象完全相同,但存储在不同的位置,将返回 false。

 

对象总是通过 引用传递。如果需要为一个已经存在的对象复制一个单独的副本,而不是提供一个指向自己的引用,可以使用 clone 关键字来创建:

<?php
class Human {
    public $name;
}

$person1 = new Human();
$person1->name = 'John';

$person2 = clone $person1;
$person2->name = 'Dee';

echo 'person one is ',$person1->name,' and person two is ',$person2->name,'.<br />';

if($person1 == $person2) {
    echo 'equiv <br />';
}else{
    echo 'not equiv <br />';
}

if($person1 === $person2){
    echo 'exact same object!';
}else{
    echo 'not the same';
}

输出:

person one is John and person two is Dee.
not equiv 
not the same

 

说明:使用 clone 关键字会从同一个类中重新创建一个对象,该对象和原始对象一样具有所有相同的属性,这两个对象之间没有链接,可以放心地进行修改。

 

PHP 中魔术方法 __clone() 可以在复制一个已经声明了的对象时调用该方法。因此可以通过声明该方法来决定复制对象时会做些什么:

<?php
class Human {
    public $name;

    public function __clone(){
        echo 'clone... <br />';
    }    
}

$person1 = new Human();
$person1->name = 'John';

$person2 = clone $person1;
$person2->name = 'Dee';

echo 'person one is ',$person1->name,' and person two is ',$person2->name,'.<br />';

if($person1 == $person2) {
    echo 'equiv <br />';
}else{
    echo 'not equiv <br />';
}

if($person1 === $person2){
    echo 'exact same object!';
}else{
    echo 'not the same';
}

输出:

clone... 
person one is John and person two is Dee.
not equiv 
not the same

 

 

由于对象总是通过引用传递,表明无需从一个方法中返回一个对象来观察它的变化,然而,可以通过从一个方法中返回 $this 在程序内建立一个流畅的接口(fluent interface),通过该接口将方法链在一起:

<?php
class Human {
    protected $name;
    protected $wight;

    public function setName($name) {
        echo 'my name is '.$name.'<br />';
        $this->name = $name;
        return $this;
    }

    public function setWeight($weight) {
        echo 'my weight is '.$weight.' kg <br />';
        $this->weight = $weight;
        return $this;
    }
}

$person = new Human();
$person->setName('dee')->setWeight('130');

输出:

my name is dee
my weight is 130 kg 

 

说明:return $this 的意思是得到从方法中返回的修正对象。

 

posted @ 2015-09-19 23:59  nemo20  阅读(334)  评论(0编辑  收藏  举报
访客数:AmazingCounters.com
2016/05/17 起统计