BASIC programming: About the problem of Singleton mode, Daniel come in and masturbate ~

Source: Internet
Author: User
BASIC programming: A question about singleton mode, Daniel come in and masturbate ~ ~ ~
Class Example
{
Save the class instance in this property
private static $instance;

The construction method is declared private, preventing the object from being created directly
Private Function __construct ()
{
Echo ' I am constructed
';
}

Singleton method
public static function Singleton ()
{
if (!isset (self:: $instance))
{
$c =__class__;
Self:: $instance =new $c;
}

Return self:: $instance;
}



Common methods in the example class
Public Function bark ()
{
Echo ' woof! ';
}

Prevent users from replicating object instances
Public Function __clone ()
{
Trigger_error (' Clone is not allowed. ', e_user_error);
}

Public function Destroy ()
{
Unset (self:: $instance);
}

}


This is an error because the constructor method is declared as private.
$test = new Example;


The following will get the singleton object of the example class
$test = Example::singleton ();
$test->bark ();

Copying an object will result in a e_user_error.
$test _clone = Clone $test;


$b =example::singleton ();

$b->destroy ();

?>


Original: http://www.php.net/manual/zh/language.oop5.patterns.php


Here I would like to ask:

1) $b =example::singleton (); is to generate a new object, or

$test and $b all point to Example::singleton () this generated object?


2) Why does my Destroy () method make an error? There is no way to destroy Example::singleton () This object or to make it null,

Does this make $test and $b become null at the same time?



------Solution--------------------
is the private function __clone bar, the release of the Singleton should not be a destructor, the concept is completely confused, should provide a separate public static interface to release Singleton objects.

1, same object
2, the top has been said, will not become null, you should not use those two variables, because the object is a reference.
------Solution--------------------
I wrote a little test.
PHP code
!--? phpclass hello{public static $i;p ublic static Fu    Nction Geti () {if (! Self:: $i instanceof Self) {self:: $i = new Self;    }//output variable some information debug_zval_dump (self:: $i);    You can comment out the self:: $i = null;    Debug_zval_dump (self:: $i); Return self:: $i;} Private Function __construct () {}private function __clone () {}} $i = Hello::geti ();d ebug_zval_dump ($i);d ebug_zval_dump ( Hello:: $i);/**output://1object (hello) #1 (0) RefCount (2) {}//2thNULL refcount (2)//3, 4NULL refcount (3) NULL RefCount (3) */
------Solution--------------------
have tried it yourself, a reference to the object itself, and a variable reference is 2 references, so null and the beginning of the $i is the case, do not know the right, but it is not important.


------Solution--------------------
discussion
2) Why does my Destroy () method go wrong? There is no way to destroy Example::singleton () This object or to make it null,
  • 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.