Is good to use static variable inside a PHP class method Is good to use static variable inside a PHP class method codeigniter codeigniter

Is good to use static variable inside a PHP class method


In your example, nothing prevents the programmer to instanciate the class more than once (with dire results), so it is rather a confusing bit of code.

Static variables have their uses, be them local to a function or defined at class level.
Especially in PHP scripts, when the output is often a single piece of data that can conveniently be handled as a class defining only static properties and methods.
That would be a true, foolproof singleton class.

Since mistaking a static variable for a dynamic one is a common pitfall, I tend to favor static class variables to avoid the confusion (i.e. the self::$... syntax makes them stand out clearly).


General consensus as far as statics are concerned in PHP is: Avoid, if at all possible. And yes, 99% of the time, it is possible to avoid statics.
Singletons should be avoided 100% of the time. For reasons you can find here and virtually everywhere else on the web. Singletons are like communism: sounds like a nice idea, but when put in to practice, it turns out there's one or two things you didn't anticipate.
A Singletons' main purpouse is to retain state, but PHP itself is stateless, so come the next request, the singleton needs to be re-initialized anyway.

If I write getters like yours, I tend to create them in a lazy-load kind of way:

class Example{    private $reviewModel = null;//create property    public function getReviewModel()    {        if ($this->reviewModel === null)        {//load once the method is called, the first time            $this->reviewModel = $this->load->model('review_model');        }        return $this->reviewModel;    }}

This basically does the same thing, without using statics. Because I'm using a property, I still retain the instance, so if the getReviewModel method is called again, the load->model call is skipped, just as it would be using a static.
However, since you're asking about performance as well as coding standards: statics are marginally slower than instance properties: Each instance has a HashTable containing for its properties, and a pointer to its definition. Statics reside in the latter, because they are shared by all instances, therefore a static property requires extra lookup work:

instance -> HashTable -> propertyinstance -> definition -> HashTable -> property

This isn't the full story, check answer + links here, but basically: the route to a static propery is longer.

As for coding standards: They exist, though still unofficial, most major players subscribe to them, and so should you: PHP-FIG
Things like $this->_protectedProperty; don't comply with the PSR-2 standard, for example, which states, quite unequivocally:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.