2012-03-14 51 views
2

我有一個服從單例模式的類的對象。我需要在一個文件中初始化它,然後在其他文件中使用它。我不知道如何做到這一點,這裏是我的嘗試:如何在PHP(5)中的文件之間共享動態變量?

//myClass.php 
class myClass 
{ 
    private static $instance = null; 

    private function __construct($args) 
    { 
     //stuff 
    } 

    public function Create($args) 
    { 
     self::$instance = new myClass($args); 
     return self::$instance; 
    } 

    public function Get() 
    { 
     return self::$instance; 
    } 
} 

//index.php 
<?php 
require_once('myClass.php'); 
$instance = myClass::Create($args); 
?> 
<a href="test.php">Test Me!</a> 

//test.php 
echo(is_null(myClass::Get())); //displays 1 

所以問題是,從test.phpmyClass::get()始終返回null!

我也嘗試將實例存儲在$_SESSION中,這給了我相同的結果。你能指點我正確的方向嗎?

+0

忘記單例,你不需要它們在PHP中。你在找什麼叫做會話。爲什麼你的會議不工作我會說這首先需要一些基本的調試。 HTTP:// PHP。net/session – hakre 2012-07-01 10:54:27

回答

0

你應該包括在每個文件中的類difinition文件它在哪裏使用(並且在使用之前應該包括它)。

<?php // filename: test.php 
include_once("myClass.php"); 
$oClassInstance = myClass::Get(); 

var_dump($oClassInstance); 

BTW

你並不需要定義這兩個方法CreateGet。您可以創建只有一個方法叫getInstance

// only one instance of the class 
private static $_oInstance = null; 

public static function getInstace() 
{ 
    if (!self::$_oInstance) 
    { 
     self::$_oInstance = new self(); 
    } 

    return self::$_oInstance; 
} 

然後你就可以使用它像:

<?php // filename: index.php 
include_once("myClass.php"); 

// if instance does not exist yet then it will be created and returned 
$oClass = myClass::getInstace(); 

<?php // filename: test.php 
include_once("myClass.php"); 

// the instance already created and stored in myClass::$_oInstance variable 
// so it just will be returned 
$oClass = myClass::getInstance(); 

UPD

如果你把一些參數到構造方法僅使用預定義參數:

private function __construct($aArg) 
{ 
    // this code will be launched once when instance is created 
    // in the any other cases you'll return already created object 
} 

public static function getInstance($aArgs = null) 
{ 
    if (!self::$_oInstance) 
    { 
     self::$_oInstance = new self($aArgs); 
    } 

    return self::$_oInstance; 
} 

ANSWER

對不起,你必須滾動幾屏找到這個=))) 爲什麼你不能在你環境中使用myClass::Get()的原因是,你有2個腳本,這意味着 - 兩個不同的程序。 應該在單個應用程序中使用單例(一個腳本)。

所以你的情況,正確的使用將是模塊系統: - 的index.php - main.php - test.php的

// file: index.php 
include_once "myClass.php" 

$module = $_GET["module"]; 
include_once $module ".php"; 

// file: main.php 
$oClass = myClass::Create($someArgs); 
var_dump($oClass); // you'll see you class body 

// file: test.php 
$oClass= myClass::Get(); 
var_dump($oClass); // you'll see the same class body as above 

而且你的鏈接將是:

  • index.php?module = main
  • index.php?module = test
+0

我忘了把include放在test.php中,但它確實存在於我的實際測試代碼中,所以問題不在此處。另外,我確實需要create函數,因爲我需要在首次調用時將參數傳遞給私有構造函數! – djfm 2012-03-27 18:31:16

+0

所以請更新問題;) – 2012-03-28 08:49:59

+0

我更新了我的答案,並添加了*答案*部分=)請檢查出來,希望這可以幫助你;) – 2012-03-28 09:15:40

0

Create()函數需要在創建新對象之前檢查$ instance屬性是否已經有一個值。例如

public function Create() 
{ 
    if (is_null(self::$instance)) { 
     self::$instance = new self(); 
    } 
    return self::$instance; 
} 

在test.php的,你可以叫MyClass的::創建(),沒有必要擁有的Get()函數在所有