首页 > 后端开发 > php教程 > PHP 魔术方法 __clone __toString(五),__clone__tostring_PHP教程

PHP 魔术方法 __clone __toString(五),__clone__tostring_PHP教程

WBOY
发布: 2016-07-13 10:13:06
原创
915 人浏览过

PHP 魔术方法 __clone __toString(五),__clone__tostring

__clone()  - 当对象克隆的时候自动加载此方法

__toString() - 当对象需要echo打印输出的时候自动加载此方法

 

__clone()

<?php
class example{
    public static $pa;
    public $pb;
    public function __construct(){
        $this->pb = ++self::$pa;
    }
    
    public function __clone(){
        $this->pb = 'no zuo no die';
    }
}

$a = new example;
$b = new example;
$c = clone $b;
$b->pb = 'I Love You So Much!';

echo $a->pb;
echo '<hr/>';
echo $b->pb;
echo '<hr/>';
echo $c->pb;
echo '<hr/>';
echo $b->pb;
?>
登录后复制

结果如下

<span>1</span>
------------------------------------------------------------------------------------<span>
I Love You So Much!
</span>------------------------------------------------------------------------------------<span>
no zuo no die /*要不是有__clone()这个魔术方法,这边的结果应该是2*/
</span>------------------------------------------------------------------------------------<span>
I Love You So Much!</span>
登录后复制

php手册给了我们一个例子有些难以看懂,如下

<?php
class SubObject
{
    static $instances = 0;
    public $instance;

    public function __construct() {
        $this->instance = ++self::$instances;
    }

    public function __clone() {
        $this->instance = ++self::$instances;
    }
}

class MyCloneable
{
    public $object1;
    public $object2;

    function __clone()
    {
      
        // 强制复制一份this->object, 否则仍然指向同一个对象
        $this->object1 = clone $this->object1;
    }
}

$obj = new MyCloneable();

$obj->object1 = new SubObject();
$obj->object2 = new SubObject();

$obj2 = clone $obj;


print("Original Object:\n");
print_r($obj);

print("Cloned Object:\n");
print_r($obj2);

?>
登录后复制

最后的结果

Original <span>Object</span><span>:
MyCloneable </span><span>Object</span><span>
(
    [object1] </span>=> SubObject <span>Object</span><span>
        (
            [instance] </span>=> <span>1</span><span>
        )

    [object2] </span>=> SubObject <span>Object</span><span>
        (
            [instance] </span>=> <span>2</span><span>
        )

)
Cloned </span><span>Object</span><span>:
MyCloneable </span><span>Object</span><span>
(
    [object1] </span>=> SubObject <span>Object</span><span>
        (
            [instance] </span>=> <span>3  <span>/*可能这里比较难以理解,其实就是$obj2当克隆的时候将最后的instance为2的结果克隆,并且再执行SubObject::__clone方法*/</span></span><span>
        )

    [object2] </span>=> SubObject <span>Object</span><span>
        (
            [instance] </span>=> <span>2</span><span>
        )

)</span>
登录后复制

__toString()

<?php
// Declare a simple class
class TestClass
{
    public $foo;

    public function __construct($foo) 
    {
        $this->foo = $foo;
    }

    public function __toString() {
        return $this->foo;
    }
}

$class = new TestClass('Hello');
echo $class;
?>
登录后复制

结果

Hello
登录后复制

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/917481.htmlTechArticlePHP 魔术方法 __clone __toString(五),__clone__tostring __clone() - 当对象克隆的时候自动加载此方法 __toString() - 当对象需要echo打印输出的时候自动加...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板