首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP文件之间共享动态变量(5)?

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

Stack Overflow用户
提问于 2012-03-15 04:05:57
回答 2查看 150关注 0票数 2

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

代码语言:javascript
复制
//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.php中,myClass::get()总是返回null!

我还尝试将实例存储在$_SESSION中,这给出了相同的结果。你能给我指出正确的方向吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-27 20:37:02

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

代码语言:javascript
复制
<?php // filename: test.php
include_once("myClass.php");
$oClassInstance = myClass::Get();

var_dump($oClassInstance);

顺便说一句

您不需要定义这两个方法CreateGet。您只能创建一个名为getInstance的方法

代码语言:javascript
复制
// only one instance of the class
private static $_oInstance = null;

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

    return self::$_oInstance;
}

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

代码语言:javascript
复制
<?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();

更新

如果你必须将一些参数放入构造函数中,只需使用预定义的参数:

代码语言:javascript
复制
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;
}

应答

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

因此,在您的情况下,正确的用法是模块系统:- index.php - main.php - test.php

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2012-03-27 20:32:50

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

代码语言:javascript
复制
public function Create()
{
    if (is_null(self::$instance)) {
        self::$instance = new self();
    }
    return self::$instance;
}

在test.php中,您可以只调用myClass::Create(),而不需要使用Get()函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9709453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档