Detailed description of object generation methods for PHP object-oriented programming, and detailed description of python Object-Oriented Programming
This article describes the object Generation Method of PHP object-oriented programming. We will share this with you for your reference. The details are as follows:
Object
Let's look at an example.
<? Phpabstract class Employee {// Employee protected $ name; function _ construct ($ name) {$ this-> name = $ name;} abstract function fire ();} class Minion extends Employee {// slave inherited Employee function fire () {print "{$ this-> name}: I'll clear my desk \ n ";}} class NastyBoss {// bad boss private $ employees = array (); function addEmployee ($ employeeName) {// Add employee $ this-> employees [] = new Minion ($ employeeName); // code spirit Activity restricted} function projectFails () {if (count ($ this-> employees)> 0) {$ emp = array_pop ($ this-> employees ); $ emp-> fire (); // fried squid }}$ boss = new NastyBoss (); $ boss-> addEmployee ("harry "); $ boss-> addEmployee ("bob"); $ boss-> addEmployee ("mary"); $ boss-> projectFails (); // output: // mary: i'll clear my desk?>
Let's look at a more flexible case
<? Phpabstract class Employee {protected $ name; function _ construct ($ name) {$ this-> name = $ name;} abstract function fire ();} class Minion extends Employee {function fire () {print "{$ this-> name}: I'll clear my desk \ n ";}} class NastyBoss {private $ employees = array (); function addEmployee (Employee $ employee) {// input object $ this-> employees [] = $ employee;} function projectFails () {if (count ($ thi S-> employees) {$ emp = array_pop ($ this-> employees); $ emp-> fire () ;}}// new Employee class... class CluedUp extends Employee {function fire () {print "{$ this-> name}: I'll call my lawyer \ n" ;}}$ boss = new NastyBoss (); $ boss-> addEmployee (new Minion ("harry"); // you can directly use an object as a parameter, more flexible $ boss-> addEmployee (new CluedUp ("bob"); $ boss-> addEmployee (new Minion ("mary ")); $ boss-> projectFails (); $ boss-> ProjectFails (); $ boss-> projectFails (); // output: // mary: I'll clear my desk // bob: i'll call my lawyer // harry: I'll clear my desk?>
Singleton
<? Phpclass Preferences {private $ props = array (); private static $ instance; // private, static property private function _ construct () {}// unable to be instantiated, private constructor public static function getInstance () {// The static method of the returned object can be accessed by the class. The static method must have the static property if (empty (self :: $ instance) {self ::$ instance = new Preferences ();} return self: $ instance;} public function setProperty ($ key, $ val) {$ this-> props [$ key] = $ val;} public function get Property ($ key) {return $ this-> props [$ key] ;}$ pref = Preferences: getInstance (); $ pref-> setProperty ("name ", "matt"); unset ($ pref); // remove the reference $ pref2 = Preferences: getInstance (); print $ pref2-> getProperty ("name "). "\ n"; // demonstrate value is not lost?>
Comment: you cannot create objects at will. You can create objects only through Preferences: getInstance.
Factory Model
<? Phpabstract class ApptEncoder {abstract function encode ();} class BloggsApptEncoder extends ApptEncoder {function encode () {return "Appointment data encoded in BloggsCal format \ n ";}} class extends ApptEncoder {function encode () {return "Appointment data encoded in MegaCal format \ n" ;}} class CommsManager {// responsible for producing the Bloggs object function getApptEncoder () {return new BloggsApptEncoder () ;}}$ Obj = new CommsManager (); $ bloggs = $ obj-> getApptEncoder (); // obtain the object print $ bloggs-> encode ();?>
Output:
Appointment data encoded in BloggsCal format
Further increase flexibility settings
<?phpabstract class ApptEncoder { abstract function encode();}class BloggsApptEncoder extends ApptEncoder { function encode() { return "Appointment data encoded in BloggsCal format\n"; }}class MegaApptEncoder extends ApptEncoder { function encode() { return "Appointment data encoded in MegaCal format\n"; }}class CommsManager { const BLOGGS = 1; const MEGA = 2; private $mode ; function __construct( $mode ) { $this->mode = $mode; } function getHeaderText() { switch ( $this->mode ) { case ( self::MEGA ): return "MegaCal header\n"; default: return "BloggsCal header\n"; } } function getApptEncoder() { switch ( $this->mode ) { case ( self::MEGA ): return new MegaApptEncoder(); default: return new BloggsApptEncoder(); } }}$man = new CommsManager( CommsManager::MEGA );print ( get_class( $man->getApptEncoder() ) )."\n";$man = new CommsManager( CommsManager::BLOGGS );print ( get_class( $man->getApptEncoder() ) )."\n";?>
Output:
MegaApptEncoder
BloggsApptEncoder
The factory method mode separates the Creator class from the product class to be produced.
Abstract Factory
Through abstraction to constrain and become a certain rule.
<? Phpabstract class ApptEncoder {abstract function encode ();} class BloggsApptEncoder extends ApptEncoder {function encode () {return "Appointment data encoded in BloggsCal format \ n ";}} class extends ApptEncoder {function encode () {return "Appointment data encoded in MegaCal format \ n" ;}} abstract class CommsManager {// reserve abstract function getHeaderText (); abstract function getfunction app TEncoder (); abstract function getTtdEncoder (); abstract function getContactEncoder (); abstract function getFooterText ();} class BloggsCommsManager extends CommsManager {function getHeaderText () {return "BloggsCal header \ n";} function getApptEncoder () {return new BloggsApptEncoder ();} function getTtdEncoder () {return new blogsttdencoder ();} function getContactEncoder () {return new BloggsCon TactEncoder ();} function getFooterText () {return "BloggsCal footer \ n" ;}} class response CommsManager extends CommsManager {function getHeaderText () {return "MegaCal header \ n ";} function getApptEncoder () {return new response apptencoder ();} function getTtdEncoder () {return new MegaTtdEncoder ();} function getContactEncoder () {return new response contactencoder ();} function getFooterText () {return "Mega Cal footer \ n ";}}$ mgr = new courier commsmanager (); print $ mgr-> getHeaderText (); print $ mgr-> getApptEncoder ()-> encode (); // call the method of the object, return the object, and continue to call the method. Print $ mgr-> getFooterText ();?>
Output:
MegaCal header
Appointment data encoded in MegaCal format
MegaCal footer
More awesome implementation
<? Php // plan the class code based on the class diagram. Start with the overall situation. Abstract class ApptEncoder {abstract function encode ();} class BloggsApptEncoder extends ApptEncoder {function encode () {return "Appointment data encoded in BloggsCal format \ n ";}} class extends ApptEncoder {function encode () {return "Appointment data encoded in MegaCal format \ n" ;}} abstract class CommsManager {const APPT = 1; const TTD = 2; const CONTACT = 3; abstract function GetHeaderText (); abstract function make ($ flag_int); // int mark abstract function getFooterText ();} class BloggsCommsManager extends CommsManager {function getHeaderText () {return "BloggsCal header \ n";} function make ($ flag_int) {switch ($ flag_int) {case self: APPT: // self directly controls the Constant return new BloggsApptEncoder (); case self: CONTACT: return new BloggsContactEncoder (); case self: TTD: return new Blogsttdencoder () ;}} function getFooterText () {return "BloggsCal footer \ n" ;}}$ mgr = new BloggsCommsManager (); print $ mgr-> getHeaderText (); print $ mgr-> make (CommsManager: APPT)-> encode (); print $ mgr-> getFooterText ();?>
Output:
BloggsCal header
Appointment data encoded in BloggsCal format
BloggsCal footer
Prototype
Transformed into a factory class that stores specific products.
<? Phpclass Sea {} // Sea class EarthSea extends Sea {} class MarsSea extends Sea {} class Plains {} // plain class EarthPlains extends Plains {} class MarsPlains extends Plains {} class Forest {}// Forest class EarthForest extends Forest {} class MarsForest extends Forest {} class TerrainFactory {// region factory private $ sea; private $ forest; private $ plains; function _ construct (Sea $ sea, Plains $ plains, Forest $ forest) {// Define the variable as a Class Object $ this-> sea = $ sea; $ this-> plains = $ plains; $ this-> forest = $ forest;} function getSea () {return clone $ this-> sea; // clone} function getPlains () {return clone $ this-> plains;} function getForest () {return clone $ this-> forest; }}$ factory = new TerrainFactory (new EarthSea (), new EarthPlains (), new EarthForest ()); print_r ($ factory-> getSea (); print_r ($ factory-> getPlains (); pri Nt_r ($ factory-> getForest ();?>
Output:
EarthSea Object()EarthPlains Object()EarthForest Object()