我希望这不是一个愚蠢的问题,但我真的找不到答案。
我有一些带有singleton函数的全局类。主要是关于小的配置参数。
class myConfig{
protected $strQuote = '"';
protected $path_delimiter = '''';
public function __get($name){
return $this->$name; // after checking if it exist etc.
}
public static function getMe(){
// do the singleton magic
return $oInstance;
}
}
这很好:
$quote = myConfig::getMe()->strQuote;
这也是:
$oConf = myConfig::getMe();
$quote = $oConf->strQuote;
$delim = $oConf->path_delimiter;
但大多数情况下只需要一个小参数,我想将其描述为:
$quote = myConfig::$strQuote;
因为一切都是神奇的方法,但我找不到任何方法来解决这个问题。我尝试过静态__get()和__callstatic()。但无法使其发挥作用。
将属性声明为静态不是一个选项。因为类将主要用作实例。
更新解决方法
我刚想到了一个肮脏的变通办法。我不确定是不是太脏了。
$quote = myConfig::strQuote();
或
$quote = myConfig::get_strQuote();
然后用__callStatic()处理
这太脏了吗?
不使用静态变量,只需将$strQuote设置为常量变量即可。然后,您可以在与静态变量相同的庄园中访问它。const STR_QUOTE = "'";
,然后您可以在类中以self::STR_QUOTE
的形式访问它,并使用类名从外部访问它。