4

Ok I'm trying to pass parameteres to the constructor of my library I created to extend the CI_Form_validation class. Anyway, here is what I'm passing from my model:

$this->load->library('MY_Form_validation', array('config' => '', 'post' => $this->input->post()));

Then here is the MY_Form_validation library:

private $post;

    public function __construct($params) {
        parent::__construct($params['config']);
        $this->post = $params['post'];
    }

But it's saying I'm not passing anything in. Here are the error messages:

Message: Missing argument 1 for MY_Form_validation::__construct(), called in H:\WD SmartWare.swstor\HALEY-HP\Source\DStable\stable\core\Loader.php on line 1099 and defined
Message: Undefined variable: params

EDIT On request, the whole class:

class MY_Form_validation extends CI_Form_validation {
    private $post;

    public function __construct($params) {
        parent::__construct($params['config']);
        $this->post = $params['post'];
    }
}
4

1 回答 1

1

您只需将空数组而不是空字符串作为第一个参数传递给构造函数,该参数将发送给父级:

$this->load->library('MY_Form_validation', array('config' => array(), 'post' => $this->input->post()));

CI 表单验证库实际上是这样初始化配置参数的,这是来自的构造函数system/libraries/Form_validation.php

public function __construct($rules = array())
于 2012-12-23T11:21:08.427 回答