如何在PHP中创建单例?
我正在用PHP5.3在EclipseIndigo上使用PDT和Aptana,我想在类中创建一个单例 所谓单例,我的意思是我只想拥有该对象的一个实例,其他对象或类通过返回该对象的函数获得该实例,这意味着我试图在定义该对象的类中创建一个对象,即:在类objA中创建objA 我明白你不能这么做:如何在PHP中创建单例?,php,eclipse,singleton,aptana,eclipse-pdt,Php,Eclipse,Singleton,Aptana,Eclipse Pdt,我正在用PHP5.3在EclipseIndigo上使用PDT和Aptana,我想在类中创建一个单例 所谓单例,我的意思是我只想拥有该对象的一个实例,其他对象或类通过返回该对象的函数获得该实例,这意味着我试图在定义该对象的类中创建一个对象,即:在类objA中创建objA 我明白你不能这么做: public $object = new Object(); 在类定义中,必须在构造函数中定义它 我怎样才能继续这样做呢?我来自Java,所以可能是我混淆了一些基本的东西。非常感谢您的帮助。代码如下: &l
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;
}
}
?>
解决了!谢谢大家的帮助 在PHP中不能这样分配类属性。它必须是标量或数组值,或者必须在方法调用中设置属性
protected static $fetcher;
static function getFetcherInstance(){
if (!self::$fetcher) {
self::$fetcher = new Fetcher();
}
return self::$fetcher;
}
另外,请注意,我没有使用$this->,因为这只适用于对象实例。要使用静态值,在类范围内工作时需要使用self::尝试以下操作:
<?php
class myclass{
private static $_instance = null;
public static function getInstance() {
if (self::$_instance === null) {
self::$_instance = new myclass();
}
return self::$_instance;
}
}
?>
并称之为:
<?php
$obj = myclass::getInstace();
?>
您可能只想阅读php站点上的常见设计模式。有很多很好的例子和很好的文档: 否则,singleton只是返回自身一个实例的方法:
class MySingletonClass {
private static $mySingleton;
public function getInstance(){
if(MySingletonClass::$mySingleton == NULL){
MySingletonClass::$mySingleton = new MySingletonClass();
}
return MySingletonClass::$mySingleton;
}
}
然后使用
MyClass::getInstance 基于@periklis answer,您可能希望为不同的应用程序范围使用单独的单例。例如,假设您想要一个单一的数据库连接-很好。但是如果你有两个数据库也需要连接呢
<?php
class Singleton
{
private static $instances = array();
public static function getInstance($name = 'default')
{
if ( ! isset(static::$instances[$name]))
{
static::$instances[$name] = new static();
}
return static::$instances[$name];
}
}
Class DB extends Singleton {}
$db_one = DB::getInstance('mysql');
$db_two = DB::getInstance('pgsql');
还可以定义克隆方法
基本上,实现单例模式意味着使用私有构造函数和静态方法编写一个类来构建自己。还要检查PHP站点:和
如果希望全局单例更改为self:$\u instance=new static;
class MyClass {
private static $instance;
public static function getInstance() {
if( !isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
<?php
class Singleton
{
private static $instances = array();
public static function getInstance($name = 'default')
{
if ( ! isset(static::$instances[$name]))
{
static::$instances[$name] = new static();
}
return static::$instances[$name];
}
}
Class DB extends Singleton {}
$db_one = DB::getInstance('mysql');
$db_two = DB::getInstance('pgsql');
class Fetcher {
protected static $instance;
private function __construct() {
/* something */
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Fetcher();
}
return self::$instance;
}
private function __clone() {
/* if we want real singleton :) */
trigger_error('Cannot clone', E_USER_ERROR);
}
}
class A {
protected $check;
private function __construct($args) {
}
static public function getSingleton($args) {
static $instance=null;
if (is_null($instance)) {
$instance=new A();
}
return $instance;
}
public function whoami() {
printf("%s\n",spl_object_hash($this));
}
}
$c=A::getSingleton("testarg");
$d=A::getSingleton("testarg");
$c->whoami(); // same object hash
$d->whoami(); // same object hash
$b= new A("otherargs"); // run time error