Welcome! Log In Create A New Profile

Advanced

singleton in php?

Posted by Jeffry Killen 
Jeffry Killen
singleton in php?
September 05, 2018 02:20AM
Hello;

I have been trying to get a better understanding of "singleton".

It is defined as a class that can only produce one instance of
itself.

In the online explanation, that is accomplished by a
private constructor function that produces an instance
of the class within its instance.

But in php I am of the understanding that a constructor
function has to be made public and can't return anything.

Here is the online reference I am looking at:
https://www.techopedia.com/definition/15830/singleton

So, at some point it seems that the class instance has to
be exported via a return statement at some point, otherwise
how would it be used in other code?

Is there a part of the manual that addresses this?

I have a hard copy text* on PHP that addresses the singleton
and factory pattern, but I have not been able to completely
comprehend either of these.

*I don't have immediate access to the book and don't remember
the title, but the publisher is Friends of Ed / (Apress).

I have been further thrown off by the use of singleton
to refer to an object literal in javascript.

I realize that this in not about javascript but the concept
and this use of the term has me confused.

More than one object literal can be created and subsequently
cloned by code, so how is it a singleton, in the general sense?

Thank you for time and attention.

JK
Tony Marston
Re: singleton in php?
September 05, 2018 10:00AM
"Jeffry Killen" wrote in message
news:[email protected]
>
>Hello;
>
>I have been trying to get a better understanding of "singleton".
>
>It is defined as a class that can only produce one instance of
>itself.
>
>In the online explanation, that is accomplished by a
>private constructor function that produces an instance
>of the class within its instance.
>
>But in php I am of the understanding that a constructor
>function has to be made public and can't return anything.
>
>Here is the online reference I am looking at:
>https://www.techopedia.com/definition/15830/singleton
>
>So, at some point it seems that the class instance has to
>be exported via a return statement at some point, otherwise
>how would it be used in other code?
>
>Is there a part of the manual that addresses this?
>
>I have a hard copy text* on PHP that addresses the singleton
>and factory pattern, but I have not been able to completely
>comprehend either of these.
>
>*I don't have immediate access to the book and don't remember
>the title, but the publisher is Friends of Ed / (Apress).
>
>I have been further thrown off by the use of singleton
>to refer to an object literal in javascript.
>
>I realize that this in not about javascript but the concept
>and this use of the term has me confused.
>
>More than one object literal can be created and subsequently
>cloned by code, so how is it a singleton, in the general sense?
>
>Thank you for time and attention.
>
>JK

There is more than one way to implement a singleton. Take a look at
http://www.tonymarston.net/php-mysql/singleton.html

--
Tony Marston
Pete Ford
Re: singleton in php?
September 05, 2018 10:00AM
On 02/09/18 21:15, Jeffry Killen wrote:
> Hello;
>
> I have been trying to get a better understanding of "singleton".
>
> It is defined as a class that can only produce one instance of
> itself.
>
> In the online explanation, that is accomplished by a
> private constructor function that produces an instance
> of the class within its instance.
>
> But in php I am of the understanding that a constructor
> function has to be made public and can't return anything.
>
> Here is the online reference I am looking at:
> https://www.techopedia.com/definition/15830/singleton
>
> So, at some point it seems that the class instance has to
> be exported via a return statement at some point, otherwise
> how would it be used in other code?
>
> Is there a part of the manual that addresses this?
>
> I have a hard copy text* on PHP that addresses the singleton
> and factory pattern, but I have not been able to completely
> comprehend either of these.
>
> *I don't have immediate access to the book and don't remember
> the title, but the publisher is Friends of Ed / (Apress).
>
> I have been further thrown off by the use of singleton
> to refer to an object literal in javascript.
>
> I realize that this in not about javascript but the concept
> and this use of the term has me confused.
>
> More than one object literal can be created and subsequently
> cloned by code, so how is it a singleton, in the general sense?
>
> Thank you for time and attention.
>
> JK

Not sure where you got the "constructor function has to be made public" from:
constructors can be private or protected in PHP just like most other OO
languages. You then hold a static instance of the singleton class, and access it
or create it with a static method.

So your basic code is something like:

class MySingleton

{

    private static $MY_INSTANCE = NULL;

    private function __construct($params)

    {

        /* Do stuff */

    }

    public static function getInstance($params)

    {

        if (self::$MY_INSTANCE===NULL)

        {

            self::$MY_INSTANCE = new MySingleton($params);

        }

        return self::$MY_INSTANCE;

    }

I think that should work - anyone else got any corrections?

Cheers

Pete
Christoph M. Becker
Re: singleton in php?
September 05, 2018 12:30PM
On 02.09.2018 at 22:15, Jeffry Killen wrote:

> I have been trying to get a better understanding of "singleton".
>
> It is defined as a class that can only produce one instance of
> itself.
>
> In the online explanation, that is accomplished by a
> private constructor function that produces an instance
> of the class within its instance.
>
> But in php I am of the understanding that a constructor
> function has to be made public and can't return anything.
>
> Here is the online reference I am looking at:
> https://www.techopedia.com/definition/15830/singleton
>
> So, at some point it seems that the class instance has to
> be exported via a return statement at some point, otherwise
> how would it be used in other code?
>
> Is there a part of the manual that addresses this?

There has been a manual page about OOP design patterns[1], but it has
been removed from the PHP manual (IMO, rightly so, since this is out of
scope). Nonetheless, the part about singletons might still be worth
reading.

[1]
http://svn.php.net/viewvc/phpdoc/en/trunk/language/oop5/patterns.xml?revision=313136&view=markup&pathrev=331456

--
Christoph M. Becker
- -
Re: singleton in php?
September 07, 2018 08:00AM
unsubscribeOn September 5, 2018 at 2:56 AM Pete Ford <[email protected]> wrote:On 02/09/18 21:15, Jeffry Killen wrote:Hello;I have been trying to get a better understanding of "singleton".It is defined as a class that can only produce one instance ofitself.In the online explanation, that is accomplished by aprivate constructor function that produces an instanceof the class within its instance.But in php I am of the understanding that a constructorfunction has to be made public and can't return anything.Here is the online reference I am looking at:https://www.techopedia.com/definition/15830/singletonSo, at some point it seems that the class instance has tobe exported via a return statement at some point, otherwisehow would it be used in other code?Is there a part of the manual that addresses this?I have a hard copy text* on PHP that addresses the singletonand factory pattern, but I have not been able to completelycomprehend either of these.*I don't have immediate access to the book and don't rememberthe title, but the publisher is Friends of Ed / (Apress).I have been further thrown off by the use of singletonto refer to an object literal in javascript.I realize that this in not about javascript but the conceptand this use of the term has me confused.More than one object literal can be created and subsequentlycloned by code, so how is it a singleton, in the general sense?Thank you for time and attention.JKNot sure where you got the "constructor function has to be made public" from: constructors can be private or protected in PHP just like most other OO languages. You then hold a static instance of the singleton class, and access it or create it with a static method.So your basic code is something like: class MySingleton {     private static $MY_INSTANCE = NULL;     private function __construct($params)     {         /* Do stuff */     }     public static function getInstance($params)     {         if (self::$MY_INSTANCE===NULL)         {             self::$MY_INSTANCE = new MySingleton($params);         }         return self::$MY_INSTANCE;     }I think that should work - anyone else got any corrections?CheersPete
- -
Re: singleton in php?
September 07, 2018 08:00AM
unsubscribeOn September 5, 2018 at 2:49 AM Tony Marston <[email protected]> wrote:"Jeffry Killen" wrote in message news:[email protected]>>Hello;>>I have been trying to get a better understanding of "singleton".>>It is defined as a class that can only produce one instance of>itself.>>In the online explanation, that is accomplished by a>private constructor function that produces an instance>of the class within its instance.>>But in php I am of the understanding that a constructor>function has to be made public and can't return anything.>>Here is the online reference I am looking at:>https://www.techopedia.com/definition/15830/singleton>>So, at some point it seems that the class instance has to>be exported via a return statement at some point, otherwise>how would it be used in other code?>>Is there a part of the manual that addresses this?>>I have a hard copy text* on PHP that addresses the singleton>and factory pattern, but I have not been able to completely>comprehend either of these.>>*I don't have immediate access to the book and don't remember>the title, but the publisher is Friends of Ed / (Apress).>>I have been further thrown off by the use of singleton>to refer to an object literal in javascript.>>I realize that this in not about javascript but the concept>and this use of the term has me confused.>>More than one object literal can be created and subsequently>cloned by code, so how is it a singleton, in the general sense?>>Thank you for time and attention.>>JKThere is more than one way to implement a singleton. Take a look at http://www.tonymarston.net/php-mysql/singleton.html-- Tony Marston
- -
Re: singleton in php?
September 07, 2018 08:00AM
unsubscribeOn September 2, 2018 at 3:15 PM Jeffry Killen <[email protected]> wrote:Hello;I have been trying to get a better understanding of "singleton".It is defined as a class that can only produce one instance of itself.In the online explanation, that is accomplished by a private constructor function that produces an instanceof the class within its instance.But in php I am of the understanding that a constructorfunction has to be made public and can't return anything.Here is the online reference I am looking at:https://www.techopedia.com/definition/15830/singletonSo, at some point it seems that the class instance has tobe exported via a return statement at some point, otherwisehow would it be used in other code?Is there a part of the manual that addresses this?I have a hard copy text* on PHP that addresses the singletonand factory pattern, but I have not been able to completelycomprehend either of these.*I don't have immediate access to the book and don't rememberthe title, but the publisher is Friends of Ed / (Apress).I have been further thrown off by the use of singletonto refer to an object literal in javascript.I realize that this in not about javascript but the conceptand this use of the term has me confused.More than one object literal can be created and subsequentlycloned by code, so how is it a singleton, in the general sense?Thank you for time and attention.JK
Sorry, only registered users may post in this forum.

Click here to login