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
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.