【发布时间】:2014-03-05 21:04:12
【问题描述】:
我的应用程序适用于第三方应用程序。第三方应用是一个库类,位于application/libraries/NAMEOFTHECLASS/main.php。
我有一个模块处理程序库,它负责处理第三方类。
第三方类如下所示:
class main {
var $name;
var $description;
var $saphir;
var $location;
public function init($config = array()) {
$this->name = $config['name'];
$this->description = $config['description'];
$this->saphir =& get_instance();
$this->location = $config['location'];
}
public function getFormElements() {
$html = $this->saphir->load->file($this->location.'/tpl/main.html',true);
$this->saphir->load->library('parser');
$data = array(
'name' => $this->name,
'description' => $this->description
);
$html = $this->saphir->parser->parse_string($html, $data, TRUE);
return $html;
}
public function validation($post_data)
{
$array = explode(',', $post_data);
if($post_data != is_array($array) )
return array("error" => array("name" => $this->name, "message" => "Bitte geben Sie nur eine Kommagetrennte Liste an"));
}
public function handleData($post_data) {
$array = explode(',', $post_data);
$return = array();
foreach($array as $key => $val) {
array_push($return, array("data" => $val));
}
return $return;
}
}
在模块处理程序中,我将我需要的每个第三方类加载到一个 foreach 中并调用一个函数。
我的 ModuleHandler 类中的 Zb 是从第三方脚本获取表单元素的方法:
private function getForm()
{
$form_data = array();
foreach($this->schema as $schema) {
$config = array("type" => $schema['type'], "name" => $schema['name'], "description" => $schema['description'], 'location' => 'application/libraries/thirdparty/cm_system/'.$schema['type']);
$type = $schema['type'];
if($this->edit) {
$value = $this->info;
$val = array();
foreach($value->ELEMENT as $element)
{
$holder = (string)$element;
array_push($val, $holder);
}
$value = $val;
}
else {
$value = "";
}
$ins = $this->CI->load->library('thirdparty/cm_system/'.$type.'/main');
$this->CI->main->init($config);
$form_elements = $this->CI->main->getFormElements($value);
array_push($form_data, $form_elements);
}
return $form_data;
}
问题是,每个第三方类都有 main 的名称。我得到了我不能重新声明主类的错误。 有没有办法取消设置加载的类?
我已经用 unset 试过了。但它不起作用。
【问题讨论】:
-
为什么不直接切换/case $type 并从同一个文件夹中加载具有不同名称但具有相同界面(策略模式)的类?工作量很小,而且比摆弄 CI 内部类更干净。
标签: php codeigniter codeigniter-2