我有一个类似下面的函数,条件语句中的每个分支都有一个代码块,我希望能够只调用那段代码然后通过数组引用它,但我是有点困惑如何做到这一点。该函数驻留在一个类中。
function do_upload()
{
$config['upload_path'] = 'assets/temp';
$config['allowed_types'] = 'csv';
$config['max_size'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
//Display errors if any
$main_data['error'] = $this->upload->display_errors();
$data['main'] = $this->load->view('adm/import_new_user_error', $main_data, TRUE);
$this->load->view('template', $data);
}
else
{
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
//Display data on uploaded file
$main_data['upload_data'] = $this->upload->data();
$data['main'] = $this->load->view('adm/import_user_sucess', $main_data, TRUE);
$this->load->view('template', $data);
}
}
这是我正在讨论的代码块。我试着把它放到静态功能但是没有用。
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
答案 0 :(得分:1)
要在程序中只使用一次公共代码,您必须更改以下代码。
function do_upload()
{
$config['upload_path'] = 'assets/temp';
$config['allowed_types'] = 'csv';
$config['max_size'] = '0';
$this->load->library('upload', $config);
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
if ( ! $this->upload->do_upload())
{
//Display errors if any
$main_data['error'] = $this->upload->display_errors();
$data['main'] = $this->load->view('adm/import_new_user_error', $main_data, TRUE);
$this->load->view('template', $data);
}
else
{
//Display data on uploaded file
$main_data['upload_data'] = $this->upload->data();
$data['main'] = $this->load->view('adm/import_user_sucess', $main_data, TRUE);
$this->load->view('template', $data);
}
}