2012-02-10 85 views
-2
<?php 

/* User info to access to db */ 

$db_host = ""; 

$db_name = ""; 

$db_user = "root"; 

$db_pass = "root"; 

/* Create an object patient */ 

class patient 
{ 
    public $name; 
    public $surname; 
    public $address; 
    public $birth_place; 
    public $province; 
    public $birth_date; 
    public $sex; 
    public $case; 

    public __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs) <-- line 26 
    { 
     $this->name = $nm; 
     $this->surname = $sur; 
     $this->address = $addr; 
     $this->birth_place = $bp; 
     $this->province = $pr; 
     $this->birth_date = $bd; 
     $this->sex = $sx; 
     $this->case = $cs; 
    } 

} 

>错误在PHP解析

我得到这个错误:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE on line 26 

为什么呢?错误在哪里?

+1

公共** **功能__construct(.... – AHHP 2012-02-10 18:17:00

回答

4

function丢失:

public function __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs) <-- line 26 
3

你忘了申报__construct()的函数。更正:

public function __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs) 
    { 
     $this->name = $nm; 
     $this->surname = $sur; 
     $this->address = $addr; 
     $this->birth_place = $bp; 
     $this->province = $pr; 
     $this->birth_date = $bd; 
     $this->sex = $sx; 
     $this->case = $cs; 
    } 
1
// Bad (missing function) 
public __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs) 

// Good 
public function __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs) 

而且,通过这个许多PARAMS到您的构造函数是不是一个好主意。我建议实施一个工厂,并传递一个数组:

// Factory 
public static function getPatient(array $array) 
{ 
    $patient = new Patient(); 

    if (array_key_exists('name', $array) { 
     $patient->setName($array['name']); 
    } 

    if (array_key_exists('surname', $array) { 
     $patient->setSurname($array['surname']); 
    } 

    return $patient; 
} 

// Calling code looks something like 
$patient = new Patient(
    array(
     'name' => $row['name'], 
     'surname' => $row['surname'] 
    ) 
); 

// Or you can simply hydrate the object after you execute your query 
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    $patient = new Patient(); 

    $patient->setName($row['name']); 
    $patient->setSurname($row['surname']); 
} 
0

public __construct必须是public function __construct

0

尝试使用:

public function __construct(... 
+0

不应该鼓励他降低范围分辨率 – 2012-02-10 18:29:11

+0

好点,谢谢。 – 2012-02-10 18:30:29