2013-07-28 112 views
0

我想做一個類,只是保存和檢索簡單的字符串變量。類內的PHP變量的範圍

以下是我在user.php的文件(類)

class User { 

//private variables 
private $u_s; 
private $p_w; 

public function _construct($u_s, $p_w){ 
    $this->u_s = $u_s; 
    $this->p_w = md5($p_w); 
} 

function getUsername(){ 
    return $this->u_s; 
} 

function getPassword(){ 
    return $this->p_w; 
} 

}

,這裏是index.php文件

<?php 
     include("User.php"); 

     $u = new User("Jake", "pass"); 
     echo $u->getUsername() + "\n" + $u->getPassword(); 
    ?> 

爲什麼這不工作?在PHP

回答

1

的毗連char是.(點),而不是+(加號):

<?php 
include("User.php"); 

$u = new User("Jake", "pass"); 
echo $u->getUsername() . "\n" . $u->getPassword(); 

正如adpalumbo說輸入錯誤__construct

2

輸入錯誤__construct。應該有兩個下劃線,而不是一個。