PHP 单例类结构 - 我做得对吗?
我有一个非常大的剧本。 它有一个数据库类、一个基类、一个用户身份验证类、一个经销商身份验证类、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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHP 中单例模式实现的示例(自 5.3 起):
使用示例:
Example of singleton pattern realization in PHP (since 5.3):
Example of usage: