Yet Another Singleton Class in PHP (

{[ .singleton | 1.hilite(=php=) ]}

Example

{[ .example | 1.hilite(=php=) ]}

Results

Now C holds world

Now A holds 3

Now AA holds world10

Now A holds 5

Now AA holds 5

AA() is A() ? No

Singleton::instance(‘A’) is A() ? Yes

One Reply to “Yet Another Singleton Class in PHP (”

  1. I think you should use the ReflectionClass to initialize the instance of $class with arguments

    Example:

    in the instance() function you call

    $instance = new $class( $args );

    in this case $args is an array and that is not what the constructor of $class expects as arguments

    Solution:

    $reflection = new ReflectionClass( $class );
    $instance = $reflection->newInstanceArgs( $args );

    Sorry for my bad english.
    I hope you understand me 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.