Php design mode: Singleton mode. The code for copying a boat. php is as follows :? Phpclassboat {privatestatic $ instancenull; private $ skipper; private $ personNum0; private $ passengersarray (); privatefunction _ boat class boat. php
The code is as follows:
Class boat {
Private static $ instance = null;
Private $ skipper;
Private $ personNum = 0;
Private $ passengers = array ();
Private function _ construct (){
}
Public static function getInstance (){
If (self: $ instance = null ){
Self: $ instance = new boat ();
}
Return self: $ instance;
}
/**
* Set the captain
*
* @ Param unknown_type $ name
*/
Public function setSkipper ($ name ){
$ This-> skipper = $ name;
}
/**
* Loader
*
* @ Param unknown_type $ person
*/
Public function load ($ person ){
If (! Emptyempty ($ person) and $ this-> personNum <= 5 ){
Array_push ($ this-> passengers, $ person );
$ This-> personNum = $ this-> personNum + 1;
}
If ($ this-> personNum> 5 ){
Echo 'this boat only load 5 person! ';
}
}
/**
* Who is mounted on the ship?
*
* @ Return unknown
*/
Public function whoInBoat (){
Return $ this-> passengers;
}
}
?>
Now let's take a look at how to attract customers.
The code is as follows:
Include 'boat. php ';
Echo "there was a river and there was only one boat in the river,
";
$ Boat1 = boat: getInstance (); // only one instance is available because there is only one ship.
Echo "John is going to Hedong now ,";
$ Boat1-> load ('Zhang San ');
Echo"
Sitting on the ship now:
";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who .'
';
}
Echo"
Now, Li Si is going to cross the river and go east.
";
$ Boat1-> load ('li si ');
Echo "sitting on the ship now:
";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who .',';
}
Echo"
Wang thought there was another ship in the river,
";
$ Boat2 = boat: getInstance ();
Echo "He is on the ship now
";
$ Boat2-> load ('Wang Wu ');
Echo "sitting on the ship now:
";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who .',';
}
?>
The pipeline code is as follows :? Php class boat {private static $ instance = null; private $ skipper; private $ personNum = 0; private $ passengers = array (); private function _...