PHP design Pattern Prototype (prototype mode) code _php Tips

Source: Internet
Author: User
Copy code code as follows:

<?php
/**
* Prototype Mode
*
* Use a prototype instance to specify the kind of object to create. And by copying this prototype to create a new pair of Like
*
*/
Abstract class Prototype
{
Private $_id = null;
Public Function __construct ($id)
{
$this->_id = $id;
}
Public Function GetID ()
{
return $this->_id;
}
Public Function __clone ()//Magic function
{
$this->_id + = 1;
}
Public Function Getclone ()
{
return clone $this;
}
}
Class Concreteprototype extends Prototype
{
}
//
$objPrototype = new Concretep Rototype (0);
$objPrototype 1 = clone $objPrototype; The
Echo $objPrototype 1->getid (). <br/> ";
$objPrototype 2 = $objPrototype; The
Echo $objPrototype 2->getid (). <br/> ";
$objPrototype 3 = $objPrototype->getclone (); The
Echo $objPrototype 3->getid (). <br/> ";

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.