2012-01-13 27 views
0

PHP 5.3でEclipse IndigoでPDTとAptanaを使用しています。クラスでシングルトンを作成したいと思います。PHPでシングルトンを作成するにはどうすればよいですか?

シングルトンとは、そのオブジェクトのインスタンスを1つだけ持ち、他のオブジェクトやクラスがそのオブジェクトを返す関数を介してその単一のインスタンスを取得するということです。あなたは、クラス定義にして

public $object = new Object(); 

:クラスOBJA)

私は理解して内OBJAの作成はあなただけの頭を移動して、これを行うことはできません。そのオブジェクト、すなわちを定義するクラス内のオブジェクトそれをコンストラクタで定義する必要があります。

どうすればこのことができますか?私はJavaから来ているので、私はいくつかの基本的なものを混乱させることができます。どんな助けでも大歓迎です。ここでは、コードです:を

<?php 
    class Fetcher{ 

    private static $fetcher = new Fetcher(); //this is where I get the unexpected "new" error 

    static function getFetcherInstance(){ 
     return $this->$fetcher; 
    } 
    } 
?> 

を解決します!すべての助けを借りてくれてありがとう!

答えて

1

PHPのようなクラスプロパティを割り当てることはできません。スカラまたは配列値でなければなりません。または、プロパティをメソッド呼び出しで設定する必要があります。

protected static $fetcher; 

static function getFetcherInstance(){ 
    if (!self::$fetcher) { 
     self::$fetcher = new Fetcher(); 
    } 
    return self::$fetcher; 
} 

また、$this->を使用しなかったことに注意してください。これはオブジェクトインスタンスでのみ機能するためです。静的な値で作業するには、クラススコープ内で作業するときにself::を使用する必要があります。

3

はこの試してみてください。

<?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(); 
?> 
+0

あなたが自己 'グローバルシングルトンの変更をしたい場合は:: $ _インスタンスは=新しい静的();' – Xeoncross

+0

が恐ろしいです。ありがとう! – yoaquim

0
<?php 
    class MyObject { 
     private static $singleInstance; 
     private function __construct() { 
     if(!isset(self::$singleInstance)) { 
      self::$singleInstance = new MyObject; 
     } 
     } 
     public static function getSingleInstance() { 
     return self::$singleInstance; 
     } 
    } 
?> 
1

あなただけのPHPのサイトで共通のデザインパターンを読みたいかもしれません。良いドキュメントとかなり良い例があります。

class MySingletonClass { 

    private static $mySingleton; 

    public function getInstance(){ 
     if(MySingletonClass::$mySingleton == NULL){ 
      MySingletonClass::$mySingleton = new MySingletonClass(); 
     } 
     return MySingletonClass::$mySingleton; 
    } 

} 
0
class MyClass { 

    private static $instance; 

    public static function getInstance() { 
     if(!isset(self::$instance)) { 
      self::$instance = new self(); 
     } 

     return self::$instance; 
    } 

} 

その後 を使用してインスタンスを取得呼び出します。そうでなければ

http://www.php.net/manual/en/language.oop5.patterns.php

は、シングルトンは、単に自身の1つのインスタンスを返すメソッドですMyClass :: getInstance();

1

@periklisの上に構築すると、異なるアプリケーションスコープに対して別々のシングルトンが必要になることがあります。たとえば、データベース接続のシングルトンを望むとしましょう。しかし、2つのデータベースを接続する必要がある場合はどうなりますか?

<?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'); 
1

Alse基本的にはシングルトンパターンを実装__clone方法

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); 
    } 
} 
1

を定義するプライベートコンストラクタと自分自身を構築するための静的メソッドを持つクラスを書くことを意味します。また、そのためのPHPサイトをチェック:http://www.php.net/manual/en/language.oop5.phphttp://it2.php.net/manual/en/book.spl.php

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 
関連する問題