PHP Singleton 扩展类
我是 OOP (PHP) 新手,刚刚接触了设计模式 - singleton
。 我找到了一个使用 mysqli (单例类)的数据库类。我向其中添加了一些自定义方法(insert_id()
、query()
、fetch_result()
等)。
然后我创建了一个名为 UserTools
的新类,我想扩展数据库类以使用我之前创建的方法(query()
、fetch_result()
等)。 但我收到这个错误:
致命错误:从 (...) 中的无效上下文调用私有 Database::__construct() 当我尝试创建新类的实例(用户工具)时。
我应该怎么办?这是一个正确的结构吗?
I am new to OOP (PHP) and just met the design pattern - singleton
.
I have found a DB class which uses mysqli (singleton class). I have added some custom methods to it (insert_id()
, query()
, fetch_result()
, etc).
Then I created a new class called UserTools
and I want to extend the database class to use the methods I've created previously (query()
, fetch_result()
, etc).
But I get this error:
Fatal error: Call to private Database::__construct() from invalid context in (...)
when I try to create instance of the new class (User Tools).
What should I do? Is it a right structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有多种方法可以实现您想要的目标。
一种是:
虽然最好像这样直接将数据库实例传递给构造函数:
如果你真的很懒,你可以修改你的数据库类并将构造函数公开:
但我真的不鼓励你使用后一种。这是非常糟糕的代码,从逻辑角度来看没有意义。您的 UserTools 类使用数据库实例。它不是数据库。
There are several way to achieve what you want.
One would be :
Although it would be better to directly pass the database instance to the constructor like this :
If you're really lazy you could just modify your database class and make the constructor public :
But I really discourage you to use the latter one. It's really bad code and it doesn't make sense in a logical point of view. Your UserTools class use a database instance. It is not a database.
据我了解,只有
protected
和public
方法和变量是通过扩展继承的,而不是私有的。尝试将您的方法/变量从private
更改为protected
。public
对所有人都可见。有关详细信息,请参阅:PHP 可见性(手动)
编辑
了解单例模式。它被称为“单例”,因为只需要一个类的一个实例。因此,大多数实现单例模式的类将构造函数定义为私有,以限制您创建多个构造函数。
为了创建单例的实例,大多数类都定义某种公共的
getInstance
静态方法。此公共方法调用私有构造函数,该构造函数可能会设置指示该类已实例化的标志,以防止进一步尝试实例化该类。getInstance
方法返回调用构造函数的结果,本质上是类的实例。It is my understanding that only
protected
andpublic
methods and variables are inherited through extension, not private ones. Try changing your methods/variables fromprivate
toprotected
.public
ones are visible to all.For more information, See: PHP Visibility (Manual)
Edit
Understand the Singleton pattern. It is called 'singleton' because only one instance of a class is expected. Because of this, most classes implementing the singleton pattern define the constructor as private to restrict you from creating more than one.
To create an instance of a singleton, most classes define some kind of
getInstance
static method, which is public. This public method calls the private constructor, which probably sets flags indiciating that the class has been instantiated in order to prevent further attempts to instantiate the class. ThegetInstance
method returns the results of calling the constructor, essentially the instance of the class.您可以编写类似于
PHP 继承的快速示例:
You could write something like
A quick example on inheritance in PHP:
在大多数情况下,单例模式通过将构造函数定义为私有(即
私有函数__construct()
)来防止实例化Singleton类。因此,如果您尝试实例化您的自定义类或您要扩展的原始类,您将收到上面的消息。您应该创建一个不同的类,或者定义并使用您的静态函数(例如
public static function query($sql, $dbconnection)
)。请参阅http://php.net/manual/en/language.oop5.patterns。 php
The singleton pattern in most cases prevents instantiating the Singleton class by defining the constructor as private (ie
private function __construct()
).So if you try to instantiate either your custom class or the original one that you're extending you will get the message above. You should either create a different class or define and use your function as static (eg
public static function query($sql, $dbconnection)
).See http://php.net/manual/en/language.oop5.patterns.php