Boat Class boat.php
Copy CodeThe 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 Captain
*
* @param unknown_type $name
*/
Public Function Setskipper ($name) {
$this->skipper= $name;
}
/**
* Loading People
*
* @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's on the boat?
*
* @return Unknown
*/
Public Function whoinboat () {
return $this->passengers;
}
}
?>
Now look how pick-up
Copy CodeThe code is as follows:
Include ' boat.php ';
echo "There once was a river, and there was only one boat in the river,
";
$boat 1=boat::getinstance ();//Because there is only one boat, so there is only one instance
echo "Now Zhang three to go to the Hedong,";
$boat 1->load (' Zhang San ');
echo "
Now sit on the boat:
";
foreach ($boat 1->whoinboat () as $who) {
echo $who. '
';
}
echo "
Now the John Doe will cross the river to the east.
";
$boat 1->load (' John Doe ');
echo "Now sits on the boat:
";
foreach ($boat 1->whoinboat () as $who) {
echo $who. ', ';
}
echo "
Harry thought the river had another boat,
";
$boat 2=boat::getinstance ();
echo "He's on board now.
";
$boat 2->load (' Harry ');
echo "Now sits on the boat:
";
foreach ($boat 1->whoinboat () as $who) {
echo $who. ', ';
}
?>
http://www.bkjia.com/PHPjc/319677.html www.bkjia.com true http://www.bkjia.com/PHPjc/319677.html techarticle Boat class boat.php Copy the code code as follows:? PHP class Boat {private static $instance =null; private $skipper; private $personNum =0; privat E $passengers =array (); Private Function _ ...