I have a relatively simple question, and although there are many posts about it on Google, I cannot find a single one that simply answers the question.
So the short question is "Is it acceptable to mix static and non static methods in one class?". I guess I am really asking "is it good practice to stick with one type of method", or "are there things to consider when using both".
For example, If I was building a class to cope with food in my fridge, which of the following (or what else) would be the best approach
Example 1:
Class Food
{
public function __construct( $itemName, $itemDescription )
{
.... some code for a new item of food ....
}
public static function getAllFood()
{
.... some code to get all the items in my fridge ....
}
}
$food = new Food( "apple", "Nice juicy Pink Lady apple" );
Food::getAllFood();
Or Example 2:
Class Food
{
public function __construct( $itemName, $itemDescription )
{
.... some code for a new item of food ....
}
public function getAllFood()
{
.... some code to get all the items in my fridge ....
}
}
$food = new Food( "apple", "Nice juicy Pink Lady apple" );
$food->getAllFood();
Thanks in advance
Fridge
which holds a collection ofFood
(and maybeDrink
etc. as well) – SarmientogetAllFoodFromDB()
to avoid confusion withgetAllFood()
from the instance of the class. – Ravishing