【发布时间】:2014-05-06 11:04:03
【问题描述】:
我收到这样的警告:
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);
如果我从以下位置更改 contor 参数:
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();
我在实例化对象时尝试将参数更改为直接参数,我尝试删除与父类的关系,但问题仍然存在。
我真的很好奇可能是什么问题,有什么想法吗?
【问题讨论】:
-
加载模型与加载库不同。我不认为 CodeIgniter 在加载模型时需要参数。忘掉这个 !我没看到你叫“新用户模型”!
标签: php parameters constructor arguments