5

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

Typhoon101
  • 2,063
  • 8
  • 32
  • 49
  • 1
    Neither. I would have a `Fridge` which holds a collection of `Food` (and maybe `Drink` etc. as well) – kero Nov 04 '14 at 23:26
  • @developerwjk. What if the static function in example 1 had code to retrieve food items from a database. That is a similar example to what I am actually trying to achieve. – Typhoon101 Nov 04 '14 at 23:42
  • In that case I think you should name the method `getAllFoodFromDB()` to avoid confusion with `getAllFood()` from the instance of the class. – developerwjk Nov 04 '14 at 23:48
  • PS: "getAllFood()" should never be "static" because it will always refer to a specific instance of a refrigerator. The alternative would be to make getAllFood static ... then pass your "refrigerator" instance as an argument. But it's better to just make it a non-static method. – FoggyDay Nov 04 '14 at 23:50

2 Answers2

8

Q: Is it acceptable to mix static and non-static methods in a class?

A: Sure, as long as:

1) You truly feel both methods logically belong in the same class, and

2) The static method(s) can/should be called independent of any class instance.

The best rule of thumb is to use static methods when the call is truly stateless.

Here's a good discussion:

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
6

In this case you have to go with example 2, because what you're trying to do in example 1 will not work:

$food = new Food( "apple", "Nice juicy Pink Lady apple" );
Food::getAllFood(); //static

There will not be any food returned unless there's some hardcoded in the class. What you put in with the constructor, you put into the instance in $food. But you're calling the class itself Food::getAllFood() to retrieve. That doesn't make sense.

Are there cases where it would make sense to include a static method in a class? Yes. Like if I was making a db connection class that would hold a connection, but I also wanted to expose some date format methods that are related to the particular DBMS but don't actually require the connection to the db to be active. Anything touching the db would have to be non-static, but some transformation methods on data I've already pulled out could be static:

$db = new JDE_DBClass($connectionString);
$rows = $db->doSelectQuery("select * from whatever");
$date = JDE_DBClass::convertJDE_Date_to_PHP_Date($rows[0]['dateField']);

In this case you might want to do this to allow conversion without having to instantiate the db object, because perhaps you might need to convert back and forth between JDE's so-called Julian format and regular dates before even determining whether you need to connect to the db:

$date = JDE_DBClass::convertJDE_Date_to_PHP_Date('114309');
developerwjk
  • 8,619
  • 2
  • 17
  • 33