PHP - 使类对象在整个应用程序中全局化?
有没有一种简单的方法可以使 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要单例对象。
检查此链接以获取更多详细信息 http:// /www.php.net/manual/en/language.oop5.patterns.php#language.oop5.patterns.singleton
You need singleton object.
Check this link for more details http://www.php.net/manual/en/language.oop5.patterns.php#language.oop5.patterns.singleton
请参阅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.
您可以使用 __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/