2014-03-27 14 views
2

我得到這樣的警告缺少參數:笨,PHP構造 - 即使它存在

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 

而笨,NetBeans中,PHP 5.4運行的應用程序。

這是模型/ 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手冊,它看起來不錯。

Loader.php上線303是這樣做的:

$CI->$name = new $model(); 

我試圖改變的參數直接的人,當實例化對象,我試圖刪除有關父類,但問題依舊。

我真的好奇,可能是什麼問題,任何想法?

+0

加載模型與加載庫不一樣。加載模型時,我不認爲CodeIgniter需要參數。忘掉這個 !我沒有看到你稱之爲「新的User_model」! –

回答

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); 
+0

您還應該查看CodeIgniter文檔以瞭解它們如何加載模型。 http://ellislab.com/codeigniter/user-guide/general/models.html –

+0

謝謝你的解釋。我試圖調整代碼,現在它可以工作。 – Puwel

+0

是的,我在他們使用的文檔中看到: $ this-> load-> model('Model_name'); $ this-> Model_name-> function(); 但我習慣於其他的OOP語言(也可以在PHP手冊中閱讀),它應該像前面所示的那樣工作。解釋後,我明白我必須堅持另一種方式。我也使用CI的CRUD操作包裝,因此如果我在此處添加此信息,我只會使它更加困惑。 – Puwel

1

你必須這樣做

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

否則你當你創建類 的對象,因爲當你創建類的對象的構造方法執行一個時間,讓你通過所有傳遞參數在創建類的對象期間的參數值,或者你必須使用上面的方法

+0

我提到它的工作,但我不喜歡它。現在我明白了爲什麼它可以工作,所以兩種解決方案都解決了這個問題。謝謝。 – Puwel