2017-10-13 67 views
0
定義類屬性

我嘗試使用方法在類中創建屬性。可以創建一個屬性,但是如何將屬性添加到屬性中?例如,我希望wachtwoord(密碼)是私人的,但名字是公開的。使用範圍從方法

當前代碼

class UserEntity extends Entity{ 

/** 
* UserEntities constructor. 
*/ 
public function __construct($user_id){ 
    parent::__construct(); 
    $this->loadDatabase(); 
    $this->get($user_id); 
} 

/** 
* Get user form database 
* 
* @param $user_id 
*/ 
private function get($user_id){ 

    $prepare = $this->database->prepare(" 
     select * from gebruiker where gebruikerid = :user_id; 
    "); 

    $prepare->bindValue(':user_id', $user_id); 
    $prepare->execute(); 
    $result = $prepare->fetch($this->fetchMethod); 

    foreach($result as $key => $value){ 
     $this->{$key} = $value; 
    } 
} 

結果(的print_r)

UserEntity Object 

( [數據庫:保護] => PDO對象 ( )

[gebruikerid] => test_val 
[0] => test_val 
[voornaam] => test_val 
[1] => test_val 
[achternaam] => test_val 
[2] => test_val 
[email] => test_val 
[3] => test_val 
[rol] => test_val 
[4] => test_val 
[wachtwoord] => test_val 
[5] => test_val 
[laatstelogin] => test_val 
[6] => test_val 
[status] => test_val 
[7] => test_val 
[registratiedatum] => test_val 
[8] => test_val 
[afbeeldingurl] => test_val 
[9] => test_val 
[initialen] => test_val 
[10] => test_val 
[geboortedatum] => test_val 
[11] => test_val 
[bsn] => test_val 
[12] => test_val 
[laatstonline] => test_val 
[13] => test_val 

謝謝!

+0

抱歉有錯誤的結果。 –

回答

2

你需要提前申報的屬性:

class UserEntity extends Entity { 

    private $wachtwoord; 
    public $firstname; 

    ... 
} 

動態添加的屬性始終是公開的。

+0

嗨Deceze,感謝您的快速回答!有用。 –