2013-03-01 116 views
0

我在PHP上有類,當調用它時,函數__construct($_POST)必須處理。__construct在調用類時出錯

__construct()功能定義爲:

// Constructor Function 
function __construct($_POST){ 
    $this->customer  = trim($_POST['customer']); 
    $this->CreateDate = date('Y/m/d');  
} 

當我呼籲類中的任何功能,其處理並插入到數據庫,但這種按摩是出現: -

Missing argument 1 for Draft::__construct(), called in .... 

什麼錯我的代碼

thankx

+0

你如何實例化你的對象? – christopher 2013-03-01 23:17:50

+0

你如何開始上課?爲什麼在你的構造函數中傳遞$ _POST?''__construct(){'而不是'function __construct($ _ POST){'將很有可能解決您的問題 – 2013-03-01 23:18:05

回答

0

當您嘗試使用保留變量,例如$_POST$_GET$_COOKIE以及「非法偏移量」時,PHP也應該發出通知。

從你的問題,似乎你不明白the difference between function parameters and arguments。你已經傳遞了一個參數,而它應該是一個參數。

此:

function __construct($_POST){ 
    $this->customer  = trim($_POST['customer']); 
    $this->CreateDate = date('Y/m/d');  
} 

應改寫爲:

function __construct($POST){ 
    $this->customer  = trim($POST['customer']); 
    $this->CreateDate = date('Y/m/d');  
} 

然後:

$object = new YourClass($_POST); 
+0

thankx @metal_fan – 2013-03-01 23:34:41

1

Two thi ngs錯誤:

  1. 您的類構造函數需要超級全局作爲參數。
  2. 你可能不參數傳遞給調用構造一個對象:

對於2號,你應該叫:

$draft = new Draft($var);

3

我很困惑,你的意圖。

$_POSTPHP superglobal,這意味着它在所有範圍內都可用。

如果您的目的是使用發佈的數據:

沒有必要把它作爲參數傳遞

如果你傳遞一個變量,你只是這麼碰巧調用$ _ POST:

更改變量的名稱。

+0

作爲第三種可能性:如果您在大部分時間嘗試使用發佈的數據,類似於'function __construct($ input_data){...}'和*用新草稿($ _ POST)調用它* – IMSoP 2013-03-01 23:23:06

0
$_post is super global variable and you are using as constructor parameter change the variable name 
function __construct($post){ 

    $this->customer  = trim($post['customer']); 
    $this->CreateDate = date('Y/m/d');  
} 

Or Second remove $_Post in constructor parameter 

function __construct(){ 

     $this->customer  = trim($_POST['customer']); 
     $this->CreateDate = date('Y/m/d');  
    }