0

我有关于 PHP 的课程,当调用它时,函数__construct($_POST)必须是进程。

__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 .... 

我的代码有什么问题

谢谢

4

4 回答 4

3

我对你的意图感到困惑。

$_POST是一个PHP 超全局,这意味着它在所有范围内都可用。

如果您的意图是使用发布的数据:

无需将其作为参数传递

如果你传递一个你恰好调用 $_POST 的变量:

更改变量的名称。

于 2013-03-01T23:19:32.837 回答
1

错了两点:

  1. 您的类构造函数将超级全局变量作为参数。
  2. 您可能不会将参数传递给构造对象的调用:

对于 2 号,您应该致电:

$draft = new Draft($var);

于 2013-03-01T23:17:54.090 回答
0

当您尝试使用保留变量时,PHP 也应该发出通知,例如 ,$_POST$_GET$_COOKIE非法偏移”。

从您的问题来看,您似乎不了解function parameters 和 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);
于 2013-03-01T23:23:35.493 回答
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');    
    }
于 2013-03-01T23:41:57.723 回答