Using new features of good PHP5.3 to realize _php tutorial of Singleton mode

Source: Internet
Author: User
5.3 can be implemented before, but the code is more cumbersome, as follows:

Class Morder extends smodel{
protected static $handle; Single example handle

Private Function __construct () {
Something
}

/**
* The method of obtaining this type of singleton, public
*
* @return Morder
*/
public static function instance () {
if (self:: $handle) {
Return self:: $handle;
}

$class = __class__;
Self:: $handle = new $class ();
Return self:: $handle;
}

Otherthing

}


5.3 Add delay static bindings (This is a very awkward word)

The code is implemented as follows

Class Smodel {
/**
* Gets a single handle that returns an instance object of a specific model class
*/
Protected static function instance () {
if (static:: $handle) {
return static:: $handle;
}

$class = Get_called_class ();
Static:: $handle = new $class ();
return static:: $handle;
}

Parent class Something

}


Class Mgoods extends smodel{
/**
* The method of obtaining this type of singleton, public
* @return Mgoods
*/
public static function instance () {
return Parent::instance ();
}
protected static $handle; Single example handle
protected function __construct () {
Something
}

Otherthing

}


By modifying, the implementation code of the subclass is reduced in part, and is implemented by the parent class

To be honest, still very troublesome, if PHP oneself realizes singleton to be good.


http://www.bkjia.com/PHPjc/678027.html www.bkjia.com true http://www.bkjia.com/PHPjc/678027.html techarticle 5.3 can be implemented before, but the code is more cumbersome, as follows: Class Morder extends smodel{protected static $handle;//single Handle Private function __construct () {// Something}/** * get this class ...

  • 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.