14

I've got a few functions that deal with cookies. Would it be a horrible idea to group them by moving them to a class of their own and use them as static methods?

Functions:

function cookie_get(){}
function cookie_set(){}
function cookie_delete(){}

Static methods:

class cookie
{
    static function get(){}
    static function set(){}
    static function delete(){}
}
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • 2
    Just be aware of the usual "static classes/singletons are the enemy of unit testing" issues you may encounter. – John Parker Jan 14 '11 at 11:29

4 Answers4

11

It would be a great idea, provided you are fully aware of the caveats involved. This is known as the Utility Pattern:

Good candidates for utility classes are convenience methods that can be grouped together functionally.

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 2
    Thanks, that's great to know. I wonder, though, why don't PHP's native functions use that. Why `str_replace()` isn't `str::replace()`? – Emanuil Rusev Jan 14 '11 at 11:29
  • 3
    PHP is a procedural language, with OOP features tacked on as an afterthought. It is all about built-in functions (with highly inconsistent naming conventions) as opposed to static helper classes which group related functions together. – karim79 Jan 14 '11 at 11:33
  • @EmanuilRusev The PHP API core is poorly designed in general but especially so when it comes to namespaces because they weren't available until the relatively recent release of PHP5. The form str::replace() would definitely be preferred but I seriously doubt anybody will retroactively add them to the core at any point in the near future. – Evan Plaice Dec 11 '12 at 23:01
9

It's actually good practice to organize functions like that. A modern alternative would be to use a namespace.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
8

Yes, that would be a horrible idea because static methods are hard to test and mock. Why not just create a real Cookie class that you can configure at runtime with those methods as regular methods.

If you just want to group those functions into a package, you can just as well use Namespaces.


Edit: Since you brought it up in the comments: yes, for any testing purposes regular functions are as untestable as statics. So your initial situation is as "horrible" as changing it to use a static class. Even the pseudo namespace is not giving you any advantage, because you already applied that to your regular functions as well. cookie_get is as good or bad as Cookie::get.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    It's too bad namespaces aren't available until PHP 5.3. – Emanuil Rusev Jan 14 '11 at 11:44
  • @Emanuil [PHP 5.2 has officially reached end of support as of Dec, 16th 2010](http://www.php.net/archive/2010.php#id2010-12-16-1). [All users are encouraged to upgrade to PHP 5.3.](http://de2.php.net/migration53) – Gordon Jan 14 '11 at 11:47
  • 3
    @Gordon, I'm sorry if it doesn't make sense, but aren't regular functions static too? I mean how is `cookie::get()` more static (or more difficult to test) than `cookie_get()`? – Emanuil Rusev Jan 14 '11 at 13:05
  • 1
    @Emanuil Actually it makes perfect sense. Because a class is declared in global scope, calling a class method statically is basically the same as calling a global function. At least for testing purposes, they are equally bad. You will always have coupling to the global scope and you cannot mock them easily. This means having the functions in the first place is as bad as turning them into static class methods. You'll gain nothing from it. But you didnt ask whether having them is a good idea, so I didnt mention it :) – Gordon Jan 14 '11 at 13:26
  • While I agree that for this particular situation static methods are the wrong choice, their use overall isn't bad. IMHO, "static methods are hard to test and mock" is an overgeneralization. If the methods are stateless, and don't rely on global state then side-effects should never be an issue. Usually I wouldn't have an issue but I have been seeing a lot of less experienced developers parroting this advice as canon without understanding what it really means. – Evan Plaice Dec 11 '12 at 22:55
3

That would be a great way of organising your code, but why use static functions, just make a class for the required functionality.

Or as said above use namespaces, but I'm not particularly familiar with the pros/cons of them.

$cookie->get() is nicer to work with than cookie_get() in my opinion

Zen
  • 7,197
  • 8
  • 35
  • 57