有時候物件要修改某些部分屬性成為新的物件的組成部分,我們借用對原型的複製來創造新的物件。
<?php class Fish{ protected $name; protected $age; protected $weight; protected $message="还是一条活鱼"; function __construct($name, $age, $weight){ $this->name = $name; $this->age = $age; $this->weight = $weight; } function say(){ echo $this->name.';'.$this->age.';'.$this->weight.';'.$this->message; } } class doBraiseFish extends Fish{ function __clone(){ $this->message = '变成红烧鱼'; } } //还是一条活鱼 $fish = new doBraiseFish('草鱼', 2, '2kg'); $Braisefish = clone($fish); $Braisefish->say();
以上就介紹了15php原型模式,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。