2017-10-13 11 views
0

メソッドを使用してクラス内に属性を作成しようとしています。属性を作成することは可能ですが、どのように属性に範囲を追加できますか?たとえば、私はwachtwoord(パスワード)をプライベートにしたいが、firstnameはパブリックにしたい。メソッドからスコープを持つクラス属性を定義する

現在のコード

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、高速回答ありがとう!できます。 –

関連する問題