【问题标题】:Codeigniter, PHP constructor - missing argument even if it is presentCodeigniter,PHP 构造函数 - 缺少参数,即使它存在
【发布时间】: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


【解决方案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);

【讨论】:

  • 您还应该查看 CodeIgniter 文档以了解它们如何加载模型。 ellislab.com/codeigniter/user-guide/general/models.html
  • 感谢您的解释。我试图调整代码,它现在可以工作了。
  • 是的,我在他们使用的文档中看到: $this->load->model('Model_name'); $this->Model_name->function();但是习惯了我期望的其他 OOP 语言(并且还阅读了 PHP 手册)它应该像前面所示的那样工作。经过解释,我明白我必须坚持另一种方式。我还在为 CI 使用 CRUD 操作包装器,所以如果我在此处添加此信息,只会让事情变得更加混乱。
  • 你可以在模型中做任何你喜欢的事情,但 CodeIgniter 更喜欢为你做类加载。我第一次使用 CI 时的想法和你一样,但它是一个很棒的框架。
【解决方案2】:

你必须这样做

public function __construct($firstname="", $lastname="", ...) {

否则你在创建类的对象时传递了参数 因为当你创建类的对象时,构造方法会执行,所以你在创建类的对象期间传递了所有参数值,或者你必须使用上面的方法

【讨论】:

  • 我提到它有效,但我不喜欢它。现在我明白了为什么它可以正常工作,因此两种解决方案都可以解决问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 2020-09-26
  • 2011-04-27
相关资源
最近更新 更多