简体   繁体   English

php Singleton模式与Abstract类和接口

[英]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. 对于数据库,我为不同的SCDB创建了Abstract类,Interface和一些适配器。 For example, Mysqli adapter has the constructor, which call the constructor of parent with settings array as parameter. 例如,Mysqli适配器具有构造函数,该构造函数以settings数组作为参数来调用parent的构造函数。 Mysqli class uses the next scheme: Mysqli类使用下一个方案:

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. 我的任务是为这种情况创建一个Singleton模式。 It good for Database, because we can create only one instance. 这对数据库很有用,因为我们只能创建一个实例。

I don't know, how create this Singleton for me. 我不知道如何为我创建此Singleton。 I want to knew, where getInstance function I should to define. 我想知道应该在哪里定义getInstance函数。

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 PHP网站上对此进行了解释

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. 关键是使用静态属性来存储对象的实例,并使构造私有,以便无法创建新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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