php - Is it acceptable to mix static and non static methods in one class? -
php - Is it acceptable to mix static and non static methods in one class? -
i have relatively simple question, , although there many posts on google, cannot find single 1 answers question.
so short question "is acceptable mix static , non static methods in 1 class?". guess asking "is practice stick 1 type of method", or "are there things consider when using both".
for example, if building class cope nutrient in fridge, of next (or else) best approach
example 1:
class nutrient { public function __construct( $itemname, $itemdescription ) { .... code new item of nutrient .... } public static function getallfood() { .... code items in fridge .... } } $food = new food( "apple", "nice juicy pinkish lady apple" ); food::getallfood();
or illustration 2:
class nutrient { public function __construct( $itemname, $itemdescription ) { .... code new item of nutrient .... } public function getallfood() { .... code items in fridge .... } } $food = new food( "apple", "nice juicy pinkish lady apple" ); $food->getallfood();
thanks in advance
q: acceptable mix static , non-static methods in class?
a: sure, long as:
1) sense both methods logically belong in same class, and
2) static method(s) can/should called independent of class instance.
the best rule of thumb utilize static methods when phone call stateless.
here's discussion:
http://verraes.net/2014/06/when-to-use-static-methods-in-php/ php class static-function
Comments
Post a Comment