现在的位置: 首页 > 综合 > 正文

php 5 clone __clone()

2013年08月15日 ⁄ 综合 ⁄ 共 0字 ⁄ 字号 评论关闭

    class Test{
        public $a = 0;
    }

    $test = new Test();
    $subtest = $test;
    echo "$test->a/n";
    echo "$subtest->a/n";

    $subtest->a = 1;
    echo "$test->a/n";
    echo "$subtest->a/n";

    $subtest2 = clone $test;
    echo "/n$test->a/n";
    echo "$subtest2->a/n";

    $subtest2->a = 2;
    echo "$test->a/n";
    echo "$subtest2->a/n";
?>
/*
    php5中,用clone关键字实现克隆操作以创建一个对象的副本。   
    另外,还可以为类定义一个名为__clone()的方法以执行特定的克隆代码,该方法在使用
    clone关键字对类实例进行克隆操作的时候会被自动调用
*/

【上篇】
【下篇】

抱歉!评论已关闭.