我收到了这样的警告:
A PHP ERROR WAS ENCOUNTERED
Severity: Warning
Message: Missing argument 1 for User_model::__construct(), called in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\CI_PP\system\core\Loader.php on line 303 and defined
Filename: models/user_model.php
Line Number: 20
在CodeIgniter,NetBeans,PHP 5.4中运行应用程序。
这是models / user_model.php的代码(请玩,请忽略避免OOP原则):
<?php
class User_model extends MY_Model {
public $_table = 'pp_user';
public $primary_key = 'u_id';
public $firstname;
public $lastname;
public $emailAddress;
public $password;
public $gender;
public $deliveryAddress;
public $address;
public $city;
public $zip;
public $country;
public $isAdmin;
//this is line nr.20:
public function __construct($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin) {
parent::__construct();
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->emailAddress = $emailAddress;
$this->password = $password; //TODO!
if ($gender == 'male') {
$this->gender = 0;
} else if ($gender == 'female') {
$this->gender = 1;
} else {
$this->gender = -1;
}
$this->deliveryAddress = $deliveryAddress;
$this->address = $address;
$this->city = $city;
$this->zip = $zip;
$this->country = $country;
$this->isAdmin = $isAdmin;
}
}
我在registration.php(控制器)中调用构造函数:
$firstname = $this->input->post('tf_first_name');
$lastname = $this->input->post('tf_last_name');
$emailAddress = $this->input->post('tf_email_address');
$password = $this->input->post('tf_password_base');
$gender = $this->input->post('tf_gender');
$address = $this->input->post('tf_address');
$deliveryAddress = $this->input->post('tf_delivery_addres');
$city = $this->input->post('tf_city');
$zip = $this->input->post('tf_zip');
$country = $this->input->post('tf_country');
$isAdmin = FALSE;
$user_instance = new User_model(
$firstname,
$lastname,
$emailAddress,
$password,
$gender,
$address,
$deliveryAddress,
$city,
$zip,
$country,
$isAdmin);
如果我改变了来自:
的控制论点public function __construct($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin)
为:
public function __construct($firstname="", $lastname="", ...) {
然后它有效,但我不喜欢这样的解决方案。我一直在网上搜索提示,但根据PHP OOP教程和PHP手册,它看起来还不错。
第303行的Loader.php是这样做的:
$CI->$name = new $model();
我尝试在实例化对象时将参数更改为直接参数,我尝试删除与父类的关系但问题仍然相同。
我真的很好奇可能是什么问题,任何想法?
答案 0 :(得分:1)
你必须这样做
public function __construct($firstname="", $lastname="", ...) {
否则,在创建类的对象时,您已经传递了参数 因为当您创建类的对象时,构造方法执行该时间,因此您在类的创建对象期间传递了所有参数值,或者您必须使用上面的方法
答案 1 :(得分:1)
这是因为CodeIgniter在加载应用程序时调用User_model :: __ construct()。 CodeIgniter必须先加载所有模型才能使用它们,这意味着您无法或不应该将参数传递给它们。您需要将该代码从__construct移动到另一个函数,例如User_model类中的add_user,您可以将数据传递到该函数中。
<?php
class User_model extends MY_Model {
public $_table = 'pp_user';
public $primary_key = 'u_id';
public $firstname;
public $lastname;
public $emailAddress;
public $password;
public $gender;
public $deliveryAddress;
public $address;
public $city;
public $zip;
public $country;
public $isAdmin;
public function __construct() {
parent::__construct();
}
public function add($firstname, $lastname, $emailAddress, $password, $gender, $address, $deliveryAddress, $city, $zip, $country, $isAdmin) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->emailAddress = $emailAddress;
$this->password = $password; //TODO!
if ($gender == 'male') {
$this->gender = 0;
} else if ($gender == 'female') {
$this->gender = 1;
} else {
$this->gender = -1;
}
$this->deliveryAddress = $deliveryAddress;
$this->address = $address;
$this->city = $city;
$this->zip = $zip;
$this->country = $country;
$this->isAdmin = $isAdmin;
}
}
然后像这样调用模型。
$firstname = $this->input->post('tf_first_name');
$lastname = $this->input->post('tf_last_name');
$emailAddress = $this->input->post('tf_email_address');
$password = $this->input->post('tf_password_base');
$gender = $this->input->post('tf_gender');
$address = $this->input->post('tf_address');
$deliveryAddress = $this->input->post('tf_delivery_addres');
$city = $this->input->post('tf_city');
$zip = $this->input->post('tf_zip');
$country = $this->input->post('tf_country');
$isAdmin = FALSE;
$this->load->model('User_model');
$this->User_model->add($firstname,
$lastname,
$emailAddress,
$password,
$gender,
$address,
$deliveryAddress,
$city,
$zip,
$country,
$isAdmin);