Jump to content

Recommended Posts

Turning the 'OOP conventions' topics into a trilogy I guess, how would you (or what's your preferred method to) implement a singleton design pattern? Mainly interested in how you'd reference the class file and name. A Zend style approach or perhaps something  a little more bespoke?

Link to comment
https://forums.phpfreaks.com/topic/187697-how-would-you-do-it-singletons/
Share on other sites

Well there are some places where a singleton comes in handy... doesn't it ?

 

MrAdam talks about ZF...

So Zend_Registry is quite useful I believe

 

I find it very useful to make sure there is only one instance for the DB connection.

 

I know you have to be careful with them though and don't use them everywhere...

I find it very useful to make sure there is only one instance for the DB connection.

 

So what happens when you need *drumroll*... two connections?

Answer: You're screwed.

 

And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it?

Answer: You're screwed.

 

And what happens when you want to change from MySQLWhatever to PostregeSQLWhatever, but the choice of adapter is hardcoded?

Answer: You're screwed.

So what happens when you need *drumroll*... two connections?

Answer: You're screwed.

although I don't see the point of having two connections for the same DB...

in that case you would not work with a singleton... obviously...

 

And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it?

Answer: You're screwed.

nope...

cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production...

 

And what happens when you want to change from MySQLWhatever to PostregeSQLWhatever, but the choice of adapter is hardcoded?

Answer: You're screwed.

sorry, but you're wrong here...

Idealy you would not hardcode this kind of stuff in your models.

Instead you would use an abstraction layer to handle this...

Ergo, one modification in your config file and you can jump from a MySQL to a PostgreSQL or to whatever DB you'd like.

What are Signletons?

 

A Singleton is a design pattern defined by the Gang of Four (GoF) to keep only one instance of an object.

 

class Singleton {
    private static $_instance = null;
    
    private function __construct() {} // disable instantiation
    private function __clone() {} // disabe cloning
    
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

 

And that's his implementation.

And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it?

Answer: You're screwed.

nope...

cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production...

 

MysqlStuffBlaBla::getInstance()

 

That's a singleton.

 

I suppose you could do this:

$adapter = 'Mysql';
$class = $adapter . 'StuffBlaBla';
$foo = $class::getInstance();
// or
$foo = call_user_func(array($adapter . 'StuffBlaBla, 'getInstance'));

 

But that's some ugly looking shit.

 

I think you're talking about a factory.

And what happens when you need to do unit testing, but the choice of adapter is hardcoded and you want to mock it?

Answer: You're screwed.

nope...

cuz if you configure you're application correctly, let's say with Zend_Config for instance, you would be able to set up one configuration for testing, and another for production...

 

MysqlStuffBlaBla::getInstance()

 

That's a singleton.

 

I suppose you could do this:

$adapter = 'Mysql';
$class = $adapter . 'StuffBlaBla';
$foo = $class::getInstance();
// or
$foo = call_user_func(array($adapter . 'StuffBlaBla, 'getInstance'));

 

But that's some ugly looking shit.

 

I think you're talking about a factory.

 

oups...

you're right...

Zend_Db is a factory...

 

sorry about that...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.