PHP design mode single-instance mode _php Tutorial

Source: Internet
Author: User
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 _ ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.