PHP Single-instance mode

Source: Internet
Author: User

Self-study and work with PHP has been three years, we all say that PHP is very simple, there is a language based on a week will, in fact, from the beginning to learn the MVC framework, thinkphp, write code did not do too much consideration, as long as can write function on the line, gradually began to confused, do not know what the use of PHP, How should lead to a master stage, how to really write high-performance code, recently began to contact the design model and Apple iOS some design patterns, the idea became clear a lot, slowly feel, write code like art, I think I just touch this art, I will be good refueling, record their own learning PHP drip, Learn to design patterns to really use, and not to tell others that it is very powerful. encourage each other A single case is literally a class with only one instance, how is it guaranteed? The class is private and guaranteed not to be new, while ensuring that the constructor and destructor are Private,__clone () and private are not replicated. So how do we invoke the method of the instance class to create a common function. To implement a singleton scenario: 1, the database, the database open connection and shutdown is very cost system resources and memory resources, and database operations are the most frequent. Instance code <?php//Private Classes Class singletondatabase{private static $instance;Private Function __construct{ mysql_connect ()//database connection function }Private Function __deconstruct{echo "This is a private construct";}Public static function getinstance () {if (! ( Self:: $instance instance self)) {Self : The $instance =new self ();}return self:: $instance;}Public function Query () {msyql_query ()//database query}}//Client code singletondatabase::getinstance ()->query ();

PHP Single-instance mode

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.