将特定变量发送到函数

时间:2014-02-25 02:57:55

标签: php codeigniter

有没有办法只将想要的变量发送给函数? 例如,我有这个功能

function upload_file($field = "thumbnail",$types = 'gif|jpg|jpeg|png|pdf',$folder = "",$size = '0',$w = '0',$h = '0', $enc = false)
    {
        if ($folder == "")
        {
            $config['upload_path'] = './Media/uploads/';
        }
        else
        {
            if (!is_dir("./Media/uploads/$folder/"))
            {
                  mkdir("./Media/uploads/$folder/");
            }
            $config['upload_path'] = "./Media/uploads/$folder/";
        }
        $config['allowed_types'] = $types;
        $config['max_size'] = $size;
        $config['max_width']  = $w;
        $config['max_height']  = $h;
        $config['encrypt_name']  = $enc;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($field))
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);

            return array(false,$error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            return array(true,$data);
        }
    }

我希望像这样使用它

中包含的设置变量

function upload_file($field = "thumbnail",$types = 'gif|jpg|jpeg|png|pdf',$folder = "",$size = '0',$w = '0',$h = '0', $enc = false)

我想通过使用此

将$ enc = false设置为$ enc = true
$data = $this->base_model->upload_file();   

有没有办法让这种情况发生?

2 个答案:

答案 0 :(得分:0)

为什么不在功能配置文件中设置$enc = true?这样$this->base_model->upload_file();将提供您想要的内容

或只是使用$enc = true

再次简单地设置参数

$data = $this->base_model->upload_file("thumbnail",'gif|jpg|jpeg|png|pdf',"",'0','0','0',true);

答案 1 :(得分:0)

如果仅更改功能签名以使默认$enc = true不是您的目标(例如,通常您希望默认保留false)但您不想要通过再次重复它们来覆盖调用中函数定义中的任何其他默认值(如果它们在开发时更改了定义等),则为这些参数传递null值:

$data = $this->base_model->upload_file( null, null, null, null, null, null, true );

任何空参数都有效地“未传递”,因此该函数采用签名中定义的默认值。最后一个参数将true传递给$enc变量,因此是唯一不是默认值的参数。