Boat. php
Copy codeThe Code is as follows:
<? Php
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.
Copy codeThe Code is as follows:
<? Php
Include 'boat. php ';
Echo "there was a river before, and there was only one boat in the river. <br> ";
$ 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 "<br> sitting on the ship now: <br> ";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who. '<br> ';
}
Echo "<br> Li Si also wants to cross the river and go east now <br> ";
$ Boat1-> load ('Li si ');
Echo "sitting on the ship now: <br> ";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who .',';
}
Echo "<br> Wang Wu thought there was another ship in the river. <br> ";
$ Boat2 = boat: getInstance ();
Echo "He is boarding the ship now <br> ";
$ Boat2-> load ('wang wu ');
Echo "sitting on the ship now: <br> ";
Foreach ($ boat1-> whoInboat () as $ who ){
Echo $ who .',';
}
?>