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

15php prototype mode

WBOY
Release: 2016-07-29 09:05:50
Original
987 people have browsed it

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();
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template