简体   繁体   中英

php Singleton pattern with Abstract class and Interface

I am developing a framework. And I have confronted with some difficulties. For Database I have created Abstract class, Interface and some Adapters for different SCDB. For example, Mysqli adapter has the constructor, which call the constructor of parent with settings array as parameter. Mysqli class uses the next scheme:

class Hybrid_Db_Adapter_Mysqli extends Hybrid_Db_Adapter_Abstract implements Hybrid_Db_Adapter_Interface {}

My task is create a Singleton pattern for this situation. It good for Database, because we can create only one instance.

I don't know, how create this Singleton for me. I want to knew, where getInstance function I should to define.

I'm biased as can be when it comes to people developing "just a little framework with my own database encapsulation classes". But anyway... ;-)

May be I can interest you in Inversion of Control Containers and the Dependency Injection pattern instead of forcing singletons on the user?

This is explained on the PHP site

The key is using a static property to store an instance of the object and making the construct private so that new instances cannot be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM