PHP - 使类对象在整个应用程序中全局化?

发布于 2024-11-24 09:48:02 字数 1472 浏览 2 评论 0原文

有没有一种简单的方法可以使 PHP 中的整个应用程序的类对象全局化?我需要我的类在整个应用程序中只能实例化一次并使其 100% 工作。

谢谢。

编辑

决定单例模式是满足我需要的最佳想法,并被要求添加一些代码,所以这里是:

在我的应用程序中使用类:

function catp_check_request(){
    $catp=catp::getInstance();

    $dlinks=array();
    $ddlinks=array();

    foreach($catp->rawlinks->link as $rawlink){
        $dellink="catp-del-{$rawlink->name}";
        array_push($dlinks,$dellink);
    }

    // More stuff..
}

声明类:

class catp {

private static $instance;
private $errors;
private $status_messages;
private $plugin_name;
private $plugin_shortname;
private $links;
private $linksurl;
private $rawlinks;

private function __construct(){}

public static function getInstance() {
    if (self::$instance == null) {
        self::$instance = new catp();
    }
    return self::$instance;
}

public function catp(){
    $this->errors=false;
    $this->plugin_name='CloakandTrack Plugin';
    $this->plugin_shortname='CaTP';
    $this->status_messages=array(
        'updated' => 'You just updated',
        'enabled' => 'You just enabled',
        'disabled' => 'You just disabled',
        'debug' => 'Plugin is in debug mode'
    );
    $this->linksurl=dirname(__FILE__).'/links.xml';
    $this->rawlinks=simplexml_load_file($this->linksurl);
    $this->links=$this->catp_parse_links($this->rawlinks);
}

// More functions..

}

Is there a simple way to make a class object global for an entire application in PHP? I need my class to only be able to be instantiated once throughout the entire application and have it working 100%.

Thanks.

EDIT

Decided the Singleton pattern is the best idea for what I need, and was requested to add a bit of code so here it is:

Using the class in my app:

function catp_check_request(){
    $catp=catp::getInstance();

    $dlinks=array();
    $ddlinks=array();

    foreach($catp->rawlinks->link as $rawlink){
        $dellink="catp-del-{$rawlink->name}";
        array_push($dlinks,$dellink);
    }

    // More stuff..
}

Declaring the class:

class catp {

private static $instance;
private $errors;
private $status_messages;
private $plugin_name;
private $plugin_shortname;
private $links;
private $linksurl;
private $rawlinks;

private function __construct(){}

public static function getInstance() {
    if (self::$instance == null) {
        self::$instance = new catp();
    }
    return self::$instance;
}

public function catp(){
    $this->errors=false;
    $this->plugin_name='CloakandTrack Plugin';
    $this->plugin_shortname='CaTP';
    $this->status_messages=array(
        'updated' => 'You just updated',
        'enabled' => 'You just enabled',
        'disabled' => 'You just disabled',
        'debug' => 'Plugin is in debug mode'
    );
    $this->linksurl=dirname(__FILE__).'/links.xml';
    $this->rawlinks=simplexml_load_file($this->linksurl);
    $this->links=$this->catp_parse_links($this->rawlinks);
}

// More functions..

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-12-01 09:48:02

你需要单例对象。

class YourClass { 
    private $_instance = null;

    public static function getInstance() {
        if (self::$_instance == null) {
            self::$_instance = new YourClass();
        }
        return self::$_instance;
    }
}

// to get your instance use
YourClass::getInstance()->...;

检查此链接以获取更多详细信息 http:// /www.php.net/manual/en/language.oop5.patterns.php#language.oop5.patterns.singleton

You need singleton object.

class YourClass { 
    private $_instance = null;

    public static function getInstance() {
        if (self::$_instance == null) {
            self::$_instance = new YourClass();
        }
        return self::$_instance;
    }
}

// to get your instance use
YourClass::getInstance()->...;

Check this link for more details http://www.php.net/manual/en/language.oop5.patterns.php#language.oop5.patterns.singleton

美胚控场 2024-12-01 09:48:02

请参阅SO 上的单例模式(在这种情况下,它被认为是不好的做法)。

您也可以考虑使用注册表

请注意,您的问题可能是架构问题,也许有更好的方法来解决您面临的问题,例如依赖注入容器。

See the singleton pattern on SO (and in which cases it is considered bad practice).

You may consider also using registry.

Note that your problem might be an architectural one and maybe there are better ways to solve the problem you face, e.g. dependency injection container.

故事灯 2024-12-01 09:48:02

您可以使用 __autoload 功能为您动态包含该类。

http://www.electrictoolbox.com/php-autoload/

You can use __autoload feature to include the class dynamically for you.

http://www.electrictoolbox.com/php-autoload/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文