使用范围从方法
问题描述:
定义类属性
我尝试使用方法在类中创建属性。可以创建一个属性,但是如何将属性添加到属性中?例如,我希望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
)
谢谢!
答
你需要提前申报的属性:
class UserEntity extends Entity {
private $wachtwoord;
public $firstname;
...
}
动态添加的属性始终是公开的。
+0
嗨Deceze,感谢您的快速回答!有用。 –
抱歉有错误的结果。 –