Dynamically add a private property to an object

后端 未结 4 1466
感动是毒
感动是毒 2020-12-11 19:40

I have a class:

class Foo {
    // Accept an assoc array and appends its indexes to the object as property
    public function extend($values){
        forea         


        
4条回答
  •  无人及你
    2020-12-11 20:12

    Just plain, bad design.

    What's the purpose of adding a private [!] field at runtime? Existent methods can't rely on such added fields, and you'd be messing with the object functionality.

    If you want your object to behave like an hashmap [i.e. you can just call $obj -> newField = $newValue], consider using magic __get and __set methods.

提交回复
热议问题