Design mode PHP5 implementation --- prototype

xiaoxiao2021-03-06  22

** / class prototype {private $ ID = null, $ name = null; public function __construct () {$ this-> id = "0"; $ this-> name = "panwei";} public function copy (prototype $ p) {$ = new prototype (); $ q-> id = $ p -> ID 1; $ q-> name = "doodoo"; returno $}}}

$ prototype = new prototype (); $ p2 = $ prototype-> copy ($ prot);

Print_R ($ prototype); Print_R ($ P2);

?>

In fact, PHP5 has a Clone function, so that our implementation is simple, the above code can be modified to

** / class prototype {private $ ID = null, $ name = null; public function __construct () {$ this-> id = "0"; $ this-> name = "panwei";} / ** * Object is called * / public function __clone () {$ this- > ID ; $ this-> name = "doodoo"; returnid $ q}}

$ prototype = new prototype (); $ p2 = clone $ prototype;

Print_R ($ prototype); Print_R ($ P2);

?>

The use of prototype mode in PHP5 becomes the implementation of the use of Clone functions and the __clone () function.

转载请注明原文地址:https://www.9cbs.com/read-45347.html

New Post(0)