Calling the variable property directly vs getter/setters - OOP Design

后端 未结 7 817
-上瘾入骨i
-上瘾入骨i 2021-01-17 09:31

I know this is probably subjective but I read this optimization page from Google for PHP and they suggest use the variable property directly without the need of getters and

相关标签:
7条回答
  • 2021-01-17 10:21

    It depends on whether $name is public or not. If it's not, you can't access/modify it directly. The trade off is exposing your class's internal data elements directly to integrators. This may be OK for some classes, but not for others.

    For example, you wouldn't necessarily want others to be able to modify the price of a product directly in a Product class.

    0 讨论(0)
提交回复
热议问题