• PHP 魔术方法 __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;
    ?>
    

     结果如下

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

    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:
    ");
    print_r($obj);
    
    print("Cloned Object:
    ");
    print_r($obj2);
    
    ?>
    

     最后的结果

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

    __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
  • 相关阅读:
    PHP学习笔记二十八【抽象类】
    PHP学习笔记二十七【重写】
    PHP学习笔记二十六【类的重载】
    PHP学习笔记二十五【类的继承】
    BZOJ4001[TJOI2015]概率论(数学、期望、生成函数、卡特兰数)
    BZOJ5091摘苹果(概率、期望)
    [Codeforces1009E]Intercity Travelling 数学题
    ZJOI2009狼和羊的故事
    洛谷P2050[NOI2012]美食节(网络流+动态加边优化)
    apk编辑器制作共存失败的一个可能的原因(第一次手动制作的教训)
  • 原文地址:https://www.cnblogs.com/uduemc/p/4122296.html
Copyright © 2020-2023  润新知