2013-03-01 5 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

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

은 다음과 같이 다시 작성해야 하는가? – christopher

+0

어떻게 수업을 시작합니까? 왜 생성자에서 $ _POST를 전달합니까?'function __construct ($ _ POST) {'대신 function __construct() {'가 문제를 해결할 것입니다. –

답변

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');  
} 

: 그리고 어떻게 개체를 인스턴스화하는

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

thankx @metal_fan –

1

두 개의 thi ngs wrong :

  1. 클래스 생성자는 인수로 super global을 사용합니다. 숫자 2의

, 당신은 호출해야합니다 :

$draft = new Draft($var);

3

나는 당신의 의도와 같은 혼란 스러워요

  • 당신은 아마 개체를 생성하기 위해 호출에 인수를 전달하지 않는다 .

    $_POSTPHP superglobal으로 모든 범위에서 사용할 수 있습니다.

    의도가 게시 된 데이터를 사용하는 경우 :

    당신이 너무 $ _POST 호출하는 일이 변수를 전달하는 경우 인수

    로 전달 할 필요가 없습니다를 :

    변수의 이름을 변경하십시오.

  • +0

    세 번째 가능성 : 대부분의 시간에 게시 된 데이터를 사용하려고하지만 앞으로 다른 배열을 허용하려는 경우 'function __construct ($ input_data) {...}'와'new draft ($ _ POST)'를 호출 * – IMSoP

    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');  
        } 
    
    관련 문제