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

后端 未结 7 818
-上瘾入骨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:15

    I would say this is really a matter of personal preference. If performance is truly that important, then I think you answered your own question.

    However, in your first example, you can still access dog::name without the getter/setter just like you do in your second example: $rover->name = 'rover'; because $name is public.

    If you specifically want to hide a class member, you would need to declare the variable private or protected and then a getter/setter would be necessary.

提交回复
热议问题