1. 程式人生 > >php在物件之外訪問其私有屬性private及保護屬性protected的特例

php在物件之外訪問其私有屬性private及保護屬性protected的特例

程式碼如下,在這種情況下php允許訪問私有及保護屬性:

class yunke
{
    protected $a = 55;
    private $b = 66;

    public function merge()
    {
        $result = clone $this;
        $result->a=88;
        $result->b=99;
        return $result;
    }
    
    public function show()
    {
        echo $this->a;
        echo $this->b;
    }
}
$test = new yunke;
$test->show();
$test2=$test->merge();
$test2->show();

輸出:

55668899