php - Is it necessary to use static method and property in this class? -


i'm studying oop in php. , i'm confused using static methods , properties.

in example use little cache example on server side. i'm not sure use static method , property.

class villa extends ci_model {   // cache   protected $cacheowner;    ...       public function owner()   {    if ( ! empty($this->cacheowner))         return $this->cacheowner; 

i think work use "static" better? or must use "static" when inherits property or method parent class?

i'm studying oop in php. , i'm confused using static methods , properties.

in example use little cache example on server side. i'm not sure use static method , property

so, how create , use static method/properties :

class villa extends ci_model {     protected static $cacheowner = null;      public static function owner()     {         if ( ! is_null(static::$cacheowner) ) {             return static::$cacheowner;         }     } } 

use as

villa::owner(); 

i think work use "static" better? or must use "static" when inherits property or method parent class?

no, it's not right @ because static considered harmful , anti-pattern. in term object oriented programming objects encapsulate state , provide methods transform state when use static properties , methods violates principle because (static) preserves same state on instantiated objects , hence it's class oriented programming, not oop. in oop classes blueprints/data type , objects things comes in play. static methods/classes doesn't facilitate unit testing because of hard dependency , hard extend. in cases singleton design pattern, static class methods/properties useful it's (singleton) considered bad design pattern , out of oop. check solid , other links provided in (see also) section.

so, try avoid static , finally, none can tell need in class you, because know going , how class related application , if need maintain same state of property/data way application's lifetime may use not sure. also, check this search result , read answers, you'll learn lot here, @ least did lot , found this article examples, helpful too.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -