Prototype mode of PHP design mode

Source: Internet
Author: User
This article introduces the content of the PHP design model of the prototype model, has a certain reference value, now share to everyone, the need for friends can refer to

prototype mode (Prototype) is not very difficult to understand.

Summing up is to create objects in a cloned way, saving the complexity of creating objects.

It can be used primarily to prevent a large amount of code redundancy resulting from duplicate creation of objects.

The recording code is as follows:

<?php  /** Abstract Prototype class * Abstract class Prototype */abstract  class Prototype  {      abstract function Cloned ();  }      /** Hero class Inherits abstract class  *  Hero *  /  class Hero extends Prototype  {public      $weapon;//Weapon Variable        function Create ()      {          echo "hero has been created, holding";      }        Clone method      function Cloned ()      {          return clone $this;//PHP Clone method      }  }

<?php  Header ("Content-type:text/html;charset=utf-8");    ------------------prototype mode index.php------------------    require_once "prototype.php";      Instantiate the Hero class as a hero  $hero 1 = new Hero ();  $hero 1->weapon = "glass harp"; Add weapon    //second time by cloning a hero to get number second  $hero 2 = $hero 1->cloned ();    $hero 1->create ();  echo "=>{$hero 1->weapon}<br/>";    Cloning copies the functions and variables inside the class  $hero 2->create ();  echo "=>{$hero 2->weapon}<br/>";



The output is:

The hero has been created, the hand is holding the
The hero has been created, the hand is holding the

Related recommendations:

PHP design mode single-instance mode

Abstract Factory of PHP design pattern

The factory method of PHP design mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.