I’ve recently been reading up articles on how to optimize code for scalability in PHP. Several of the articles I’ve read today have discouraged the use of additional methods to simply return objects from a class.
So basically, they say:
If you have a class like this:
class myClass
{
public $something;
public function setSomething($val)
{
$this->something=$val;
}//function end
}//class end
$myClassInstance=new myClass;
And you want to get the class property $something, you should do this:
//echo $something from myClass
echo $myClassInstance->something;
And not this:
//echo $something from myClass using an additional method (getSomething()) that returns the property
echo $myClassInstance->getSomething();
Because there is a speed difference. The discouraged method is slower ($myClassInstance->getSomething()) which is why it is discouraged.
But, I see so many people still using the discouraged method (tutorials, code examples, ect). I could understand if they had to have the property set to private for whatever reason, but this is generally not the case.
So my question is, is there a benefit or something that I am missing to using the discouraged method? If so, what?
What if you wanted to apply some logic to your “GetTitle” later such as filters or change some of the output by introducing special logic? You never know when these things will start popping out.
Additionnaly, people saying the function accessor style is slower are using a 1 million loops to benchmark it, but seriously, are you going to display 1 million titles on a page?