Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions vs. Static Methods

Tags:

php

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(){}
}
like image 793
Emanuil Rusev Avatar asked Jan 14 '11 11:01

Emanuil Rusev


People also ask

Are static methods same as functions?

Static methods are not exactly functions, the difference is subtle, but important. A static method using only given input parameters is essentially a function.

What is the difference between static function and function static?

When a simple 'function' is called from different objects then all the internal variables are initialized to their default values for each individual object's function call. 'function static' is another type of function that makes the scope of its internal variables as static.

Are static functions better?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.

Are functions static?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.


4 Answers

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.

like image 82
karim79 Avatar answered Oct 02 '22 04:10

karim79


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

like image 22
Michael Borgwardt Avatar answered Oct 02 '22 03:10

Michael Borgwardt


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.

like image 34
Gordon Avatar answered Oct 02 '22 05:10

Gordon


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

like image 28
Zen Avatar answered Oct 02 '22 05:10

Zen