PHP 以 class 为值提取数组
PHP Extracting array with class as value
我正在开发一个 MVC 框架,但我在声明助手 类
时遇到了灵活 code/Structure 的问题
class Controller {
public $helper = [];
public function load_helper($helper) {
require_once(DIR_HELPER . $helper . '.php');
$lc_helper = StrToLower($helper);
$helper_arr[$lc_helper] = new $helper;
$this->helper[$lc_helper] = $helper_arr[$lc_helper];
}
}
//我在控制器中这样调用函数
Class Home Extends Controller {
$this->load_helper('Form');
$this->helper['form']-><class function>;
}
我想这样调用函数:
$this->form-><class function>;
我不能对 public 函数使用提取,但我见过可以做到这一点的框架。
我希望有人有想法并且有人可以理解我的问题,在此先感谢。
看看神奇的__get
方法。来自文档:
Overloading in PHP provides means to dynamically "create" properties
and methods. These dynamic entities are processed via magic methods
one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties
or methods that have not been declared or are not visible in the
current scope. The rest of this section will use the terms
"inaccessible properties" and "inaccessible methods" to refer to this
combination of declaration and visibility.
例如可以这样实现:
class Controller {
public $helper = [];
public function load_helper($helper) {
require_once(DIR_HELPER . $helper . '.php');
$lc_helper = StrToLower($helper);
$helper_arr[$lc_helper] = new $helper;
$this->helper[$lc_helper] = $helper_arr[$lc_helper];
}
public function __get($property) {
//Load helper if not exists
if (!isset($this->helper[$property])) {
$this->load_helper($property);
}
//Return the helper
return $this->helper[$property];
}
}
旁注:
Controller::$helper
和Controller::load_helper()
在我的理解中应该是private
或者protected
而不是public
.
我正在开发一个 MVC 框架,但我在声明助手 类
时遇到了灵活 code/Structure 的问题class Controller {
public $helper = [];
public function load_helper($helper) {
require_once(DIR_HELPER . $helper . '.php');
$lc_helper = StrToLower($helper);
$helper_arr[$lc_helper] = new $helper;
$this->helper[$lc_helper] = $helper_arr[$lc_helper];
}
}
//我在控制器中这样调用函数
Class Home Extends Controller {
$this->load_helper('Form');
$this->helper['form']-><class function>;
}
我想这样调用函数:
$this->form-><class function>;
我不能对 public 函数使用提取,但我见过可以做到这一点的框架。
我希望有人有想法并且有人可以理解我的问题,在此先感谢。
看看神奇的__get
方法。来自文档:
Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.
例如可以这样实现:
class Controller {
public $helper = [];
public function load_helper($helper) {
require_once(DIR_HELPER . $helper . '.php');
$lc_helper = StrToLower($helper);
$helper_arr[$lc_helper] = new $helper;
$this->helper[$lc_helper] = $helper_arr[$lc_helper];
}
public function __get($property) {
//Load helper if not exists
if (!isset($this->helper[$property])) {
$this->load_helper($property);
}
//Return the helper
return $this->helper[$property];
}
}
旁注:
Controller::$helper
和Controller::load_helper()
在我的理解中应该是private
或者protected
而不是public
.