Home >Backend Development >PHP Tutorial >15php prototype mode

15php prototype mode

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:05:501032browse

Sometimes an object needs to modify some of its attributes to become a component of a new object. We create a new object by cloning the prototype.

<?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();

The above has introduced the 15php prototype mode, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn