Single case mode (PHP) ____php

Source: Internet
Author: User

<?php
Final  class Singleton
{
    /**
     * @var This class of * Single example *
    /private static $instance;
    
    /** *
     Returns a single example of this class * * *
     @return Singleton the *singleton* instance.
     */public
    static function getinstance ()
    {
        if (null = = static:: $instance) {
            static:: $instance = new Static ();
        
        return static:: $instance;
    }

    /**
     * Declares the constructor as protected to prevent new instances from being created outside this class with the ' new ' operator
    /Private Function __construct ()
    {
    }

    /**
     * The Clone method is declared private to prevent the cloning of a single case
     *
     * @return void
    /Private Function __clone ()
    {
    }

    /**
     * Declares the deserialization method private to prevent deserialization of a single instance
     *
     * @return void/
    Private Function __ Wakeup ()
    {
    }
}

$obj = Singleton::getinstance ();
Var_dump ($obj = = = Singleton::getinstance ());             BOOL (TRUE)



Turn from: http://laravel-china.github.io/php-the-right-way/pages/Design-Patterns.html

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.