PHP Readonly Properties?

前端 未结 6 1206
闹比i
闹比i 2020-12-05 06:12

In using PHP\'s DOM classes (DOMNode, DOMEElement, etc) I have noticed that they possess truly readonly properties. For example, I can read the $nodeName property of a DOMNo

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 06:45

    But private properties exposed only using __get() aren't visible to functions that enumerate an object's members - json_encode() for example.

    I regularly pass PHP objects to Javascript using json_encode() as it seems to be a good way to pass complex structures with lots of data populated from a database. I have to use public properties in these objects so that this data is populated through to the Javascript that uses it, but this means that those properties have to be public (and therefore run the risk that another programmer not on the same wavelength (or probably myself after a bad night) might modify them directly). If I make them private and use __get() and __set(), then json_encode() doesn't see them.

    Wouldn't it be nice to have a "readonly" accessibility keyword?

提交回复
热议问题