PHP 单例类结构 - 我做得对吗?

发布于 2024-09-17 18:43:13 字数 1680 浏览 4 评论 0原文

我有一个非常大的剧本。 它有一个数据库类、一个基类、一个用户身份验证类、一个经销商身份验证类、paypal ipn 类、一个下载类和一个插件类。

基类扩展了数据库类,如下所示:

class Base extends database
{
    var $userid;
    var $root;
    var $config

    function Base($username, $password, $host, $database)
    {
        // Init DB
        $this -> database($hostname, $database, $username, $password);
        $this -> config = $this -> fetch(...);
        // and the code goes on
    }
    function anyFunction()
    {
        $this -> Downloads -> dloadFunction();
    }
}
class users extends Base
{
    var $userData;

    function users()
    {
        // initialize user, check if he is logged in, if he has cookies, load the user vars in $this -> userData
    }
    function userFunction1()
    {
        $this -> anyFunction();
    }
}
class resellers extends Base
{
    var $resellerData;

    function resellers()
    {
        // initialize resellers, check if he is logged in, if he has cookies, load the user vars in $this -> resellerData
    }
}
class IPN extends Base
{

}
class Downloads extends Base
{
    function dloadFunction()
    {

    }
}
class Plugins extends Downloads
{

}
?>

我这样称呼我的代码:

<?php
    $Base = new Base($user, $pass, $host, $db);
    $user = new user();
    $Base -> user = $user;
    $reseller = new reseller();
    $Base -> reseller = $reseller;
    $downloads = new Downloads();
    $downloads -> Plugins = new Plugins();
    $Base -> Downloads = $downloads;

    $Base -> users -> updateEmail();
    // and the code goes on..
?>

我认为结构非常糟糕。 这就是为什么我想实现单例方法。 我怎样才能做到这一点?

请帮忙。

I have a really big script.
It has a database class, a base class, a user authentication class, a resellers authentication class, paypal ipn class, a downloads class, and a plugins class

The Base class extends database class like this:

class Base extends database
{
    var $userid;
    var $root;
    var $config

    function Base($username, $password, $host, $database)
    {
        // Init DB
        $this -> database($hostname, $database, $username, $password);
        $this -> config = $this -> fetch(...);
        // and the code goes on
    }
    function anyFunction()
    {
        $this -> Downloads -> dloadFunction();
    }
}
class users extends Base
{
    var $userData;

    function users()
    {
        // initialize user, check if he is logged in, if he has cookies, load the user vars in $this -> userData
    }
    function userFunction1()
    {
        $this -> anyFunction();
    }
}
class resellers extends Base
{
    var $resellerData;

    function resellers()
    {
        // initialize resellers, check if he is logged in, if he has cookies, load the user vars in $this -> resellerData
    }
}
class IPN extends Base
{

}
class Downloads extends Base
{
    function dloadFunction()
    {

    }
}
class Plugins extends Downloads
{

}
?>

And I call my code like this :

<?php
    $Base = new Base($user, $pass, $host, $db);
    $user = new user();
    $Base -> user = $user;
    $reseller = new reseller();
    $Base -> reseller = $reseller;
    $downloads = new Downloads();
    $downloads -> Plugins = new Plugins();
    $Base -> Downloads = $downloads;

    $Base -> users -> updateEmail();
    // and the code goes on..
?>

I think the structure is really bad.
Thats why I want to implement singleton method.
how can I achieve this ?

Please help.

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

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

发布评论

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

评论(1

递刀给你 2024-09-24 18:43:13

PHP 中单例模式实现的示例(自 5.3 起):

/**
 * Singleton pattern implementation
 */
abstract class Singleton {

    /**
     * Collection of instances
     * @var array
     */
    private static $_aInstance = array();

    /**
     * Private constructor
     */
    private function __construct(){}

    /**
     * Get instance of class
     */
    public static function getInstance() {

    // Get name of current class
    $sClassName = get_called_class();

    // Create new instance if necessary
    if( !isset( self::$_aInstance[ $sClassName ] ) )
        self::$_aInstance[ $sClassName ] = new $sClassName();
        $oInstance = self::$_aInstance[ $sClassName ];

        return $oInstance;
    }

    /**
     * Private final clone method
     */
    final private function __clone(){}
}

使用示例:

class Example extends Singleton {}
$oExample1 = Example::getInstance();
$oExample2 = Example::getInstance();
echo ( is_a( $oExample1, 'Example' ) && $oExample1 === $oExample2)
    ? 'Same' : 'Different', "\n"; 

Example of singleton pattern realization in PHP (since 5.3):

/**
 * Singleton pattern implementation
 */
abstract class Singleton {

    /**
     * Collection of instances
     * @var array
     */
    private static $_aInstance = array();

    /**
     * Private constructor
     */
    private function __construct(){}

    /**
     * Get instance of class
     */
    public static function getInstance() {

    // Get name of current class
    $sClassName = get_called_class();

    // Create new instance if necessary
    if( !isset( self::$_aInstance[ $sClassName ] ) )
        self::$_aInstance[ $sClassName ] = new $sClassName();
        $oInstance = self::$_aInstance[ $sClassName ];

        return $oInstance;
    }

    /**
     * Private final clone method
     */
    final private function __clone(){}
}

Example of usage:

class Example extends Singleton {}
$oExample1 = Example::getInstance();
$oExample2 = Example::getInstance();
echo ( is_a( $oExample1, 'Example' ) && $oExample1 === $oExample2)
    ? 'Same' : 'Different', "\n"; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文