如何在PHP(5)中的文件之间共享动态变量?

如何在PHP(5)中的文件之间共享动态变量?,php,session,singleton,Php,Session,Singleton,我有一个遵循单例模式的类的对象。我需要在一个文件中初始化它,然后在其他文件中使用它。我不知道怎么做,以下是我尝试过的: //myClass.php class myClass { private static $instance = null; private function __construct($args) { //stuff } public function Create($args) { self:

我有一个遵循单例模式的类的对象。我需要在一个文件中初始化它,然后在其他文件中使用它。我不知道怎么做,以下是我尝试过的:

//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
//myClass.php
类myClass
{
私有静态$instance=null;
私有函数构造($args)
{
//东西
}
公共函数创建($args)
{
self::$instance=newmyclass($args);
返回self::$instance;
}
公共函数Get()
{
返回self::$instance;
}
}
//index.php
//test.php
echo(为null(myClass::Get())//显示1
所以问题是,从
test.php
myClass::get()
总是返回null

我还尝试将该实例存储在
$\u会话中
,这给了我相同的结果。您能为我指出正确的方向吗?

在创建新对象之前,Create()函数需要检查$instance属性是否已经有值。比如说

public function Create()
{
    if (is_null(self::$instance)) {
        self::$instance = new self();
    }
    return self::$instance;
}
在test.php中,您只需调用myClass::Create(),根本不需要Get()函数

Create()函数需要在创建新对象之前检查$instance属性是否已经有值。比如说

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

在test.php中,您只需调用myClass::Create(),根本不需要Get()函数

您应该在使用该文件的每个文件中包含带有类定义的文件(并且应该在使用该文件之前包含该文件)

然后你可以像这样使用它:

<?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();
回答

很抱歉,您必须滚动几个屏幕才能找到此=)) 您不能在上下文中使用
myClass::Get()
的原因是您有两个脚本,这意味着两个不同的程序。 Singleton应该在单个应用程序(一个脚本)中使用

因此,在您的情况下,正确使用模块系统: -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
您的链接将是:

  • php?模块=main
  • php?模块=测试

您应该在使用的每个文件中包含带有类定义的文件(并且应该在使用之前包含)

然后你可以像这样使用它:

<?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();
回答

很抱歉,您必须滚动几个屏幕才能找到此=)) 您不能在上下文中使用
myClass::Get()
的原因是您有两个脚本,这意味着两个不同的程序。 Singleton应该在单个应用程序(一个脚本)中使用

因此,在您的情况下,正确使用模块系统: -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
您的链接将是:

  • php?模块=main
  • php?模块=测试

我忘了将include放在test.php中,但它确实存在于我的实际测试代码中,因此问题并非来自这里。另外,我确实需要create函数,因为我需要在第一次调用时将参数传递给私有构造函数!我更新了我的答案并添加了答案部分=)请查看,希望这对您有所帮助;)我忘了将include放在test.php中,但它确实存在于我的实际测试代码中,所以问题不是从这里来的。另外,我确实需要create函数,因为我需要在第一次调用时将参数传递给私有构造函数!我更新了我的答案并添加了答案部分=)请查看,希望这对您有所帮助;)忘记singleton吧,PHP中不需要它们。你要找的是会话。为什么您的会话不能工作我想说这首先需要一些基本的调试。忘记singleton吧,PHP中不需要它们。你要找的是会话。为什么您的会话不能工作我想说这首先需要一些基本的调试。