1
此聲明的飛行性能:PHP使私有財產,因爲他們得到聲明
class metadata {
function __construct($file) {
/* Argument: Array containing data of a single file */
while ($pointer = key($file)) {
$this->$pointer = current($file);
next($file);
}
}
}
我想這是在while循環$this->$pointer
聲明的所有屬性是私有的。
我該如何做到這一點,而不需要設置很長的private $prop1, $prop2, $etc;
?
主要目的是保持代碼簡潔。我寫的很可能需要20個私人物品,我只是想知道我是否可以保存打字。
我相信你只能做,如果你預先聲明數組作爲私人&然後添加鍵,由於這裏解釋。 http://stackoverflow.com/a/1920524/117259 – JakeGould
可能的重複[動態添加一個私有屬性到一個對象](http://stackoverflow.com/questions/13415023/dynamically-add-a-private-property-到一個對象) –