When should I use stdClass and when should I use an array in php oo code?

前端 未结 7 511
太阳男子
太阳男子 2020-11-29 00:33

In the middle of a period of big refactorings at work, I wish to introduce stdClass ***** as a way to return data from functions and I\'m trying to find non-subjectives argu

7条回答
  •  旧时难觅i
    2020-11-29 00:45

    Well, there are 3 differences:

    • they have an identity. which is why the default for passing array arguments is call by value and for objects call by sharing.
    • there is a semantical difference. If you use an object, anyone who reads the code understand, that the value represents the model some sort of entitity, while an array is supposed to act as a collection or a map
    • And last but not least, refactoring becomes signifficantly easier. If you want to use a concrete class rather than stdClass, all you have to do is to instantiate another class. Which also allows you to add methods.

    greetz
    back2dos

提交回复
热议问题