2012-03-14 48 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()函数在所有