有时候对象要修改某些部分属性成为新的对象的组成部分,我们借用对原型的克隆创建新的对象。
<?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教程有兴趣的朋友有所帮助。