Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawbacks of static methods in PHP
    primarykey
    data
    text
    <p>In a theoretical database access class, I found that there are quite a few helper functions that I use in the class, which have nothing to do the class's instance (and others, that could be manipulated to be unrelated to the class's instance using dependency injection).</p> <p>For example, I have a function that gets a string between two other strings in a variable. I've been thinking of moving that to a String_Helper class, or something of the sort. This function has already been made static.</p> <p>Also, I have a function that queries a database, <code>query($sql)</code>. The connection details are provided by the instance, but I've been considering making it static, and using <code>query($sql, $connection)</code>. Developers would then be able to call it statically and not need to instantiate the database class at all.</p> <p>For me, the questions are:</p> <ol> <li><p>Is it worth it to do something like this? Functions like the query function make me wonder if this is not just me trying to make everything as static as possible, without any real need to. Under what circumstances would you consider this useful?</p></li> <li><p>I know static functions are harder to test, but if I make sure that their code is completely dependency free (or uses dependency injection where necessary), then they're just as easy to test as everything else, surely?</p></li> <li><p>It isn't a concern at the moment, but if, in the future, I decided to extend the classes with the static functions, it would be impossible for me to make the current code use my extended functions. I've thought of Singletons, but the same problem arises: the code would be calling <code>Singleton_Class::getInstance()</code>, and not <code>My_Extended_Singleton_Class::getInstance()</code>. Dependency Injection seems to be the only way to solve this issue, but it might lead to a clunkier API, as every dependency has to be given to an object on <code>__construct()</code>.</p></li> <li><p>I have a container class, which holds certain pieces of information statically so that they can be accessed anywhere in the script (global scope). If I can't use static functions or singletons, a class that contained instances of different variables would be great. One could use for example <code>Container::$objects['MyClass'] = $MyClass_object;</code>, and then the rest of the code could just access <code>Container::$objects['MyClass']</code>. If I extended the MyClass class, I could use <code>Container::$objects['MyClass'] = $MyExtendedClass_object;</code>, and the code that used <code>Container::$objects['MyClass']</code> would use MyExtendedClass, rather than MyClass. This is by far the best way to do it, in my opinion, but I'd like to know what you think about it.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload