如何在 PHP 中创建单例?
我在 Eclipse Indigo 上使用 PDT 和 Aptana 以及 PHP 5.3,我想在类中创建一个单例。
通过单例,我的意思是我只想拥有该对象的一个实例,而其他对象或类则通过返回该对象的函数来获取该单个实例(因此这意味着我正在尝试在类中创建一个对象定义该对象,即:在类 objA 中创建 objA)
我知道你不能直接这样做:
public $object = new Object();
在类定义中,你必须在构造函数中定义它。
我怎样才能继续做到这一点?我来自 Java,所以可能我混淆了一些基本的东西。非常感谢任何帮助。代码如下:
<?php
class Fetcher{
private static $fetcher = new Fetcher(); //this is where I get the unexpected "new" error
static function getFetcherInstance(){
return $this->$fetcher;
}
}
?>
已解决!感谢大家的帮助!
I'm using PDT and Aptana on Eclipse Indigo with PHP 5.3 and I want to create a singleton in a class.
By singleton, I mean I want to just have one instance of that object, and for other objects or classes to get that single instance via a function that returns that object (so this would mean I'm trying to create an object within the class that defines that object, ie: creating objA within the class objA)
I understand you can't just go a head and do this:
public $object = new Object();
with in a class definition, you have to define it in the constructor.
How can I go ahead and do this? I'm coming from Java, so it could be I'm confusing some basic stuff. Any help is greatly appreciated. Here's the code:
<?php
class Fetcher{
private static $fetcher = new Fetcher(); //this is where I get the unexpected "new" error
static function getFetcherInstance(){
return $this->$fetcher;
}
}
?>
Solved! Thanks for all the help guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个:
并用以下方式调用它:
try this:
and call it with:
还定义 __clone 方法
Alse define
__clone
method您不能像这样在 PHP 中分配类属性。它必须是标量或数组值,或者必须在方法调用中设置该属性。
另外,请注意,我没有使用
$this->
,因为它仅适用于对象实例。要使用静态值,您需要在类范围内使用self::
。You cannot assign a class property in PHP like that. It must be a scalar, or array value, or the property must be set in a method call.
Also, notice that I did not use
$this->
, as that only works for object instances. To work with static values you need to useself::
when working within the class scope.您可能只想阅读 php 站点上的常见设计模式。有很好的示例和良好的文档:
http://www.php。 net/manual/en/language.oop5.patterns.php
另外,单例只是一种返回其自身的单个实例的方法:
You might want to just read common design patterns on the php site. There are pretty good examples with good documentation:
http://www.php.net/manual/en/language.oop5.patterns.php
Else, a singleton is simply a method that returns one single instance of itself:
基于 @periklis 答案,您可能需要针对不同的应用程序范围使用单独的单例。例如,假设您想要一个数据库连接的单例 - 很好。但是,如果您还需要连接两个数据库怎么办?
Building on @periklis answer you might want separate singletons for different application scopes. For example, lets say you want a singleton of a database connection - fine. But what if you have TWO databases you need to connect too?
基本上实现单例模式意味着编写一个带有私有构造函数和静态方法来构建自身的类。另请检查 PHP 网站: http://www.php.net/manual /en/language.oop5.php 和 https://www.php.net/manual/en/book.spl.php
Basically implementing a singleton pattern means writing a class with a private constructor and a static method to build itself. Also check PHP site for it: http://www.php.net/manual/en/language.oop5.php and https://www.php.net/manual/en/book.spl.php
然后使用调用获取实例
MyClass::getInstance();
Then call get instance using
MyClass::getInstance();