in PHP, when should I use static methods vs abstract classes?

abstract-class, php, static-methods

Solution

PHP does not have static identifier on classes, so other methods are needed to prevent one from being instantiated.

You can prevent a class from being instantiated by defining it abstract, and it is a cheap way to do so, although that's not the purpose of an abstract class.

Other methods include defining the contructor private

private function __construct() {}

Or throwing an exception in the contructor if you wish to give a more meaningful message as to why it can't be instantiated.

function __construct() { throw new Exception('This is a static class'); }

If you also do not want the class subclassed declare the class final.

final class foo { }

Or in the odd case you want to be able to subclass it, but not allow any of it's children to instantiate declare the constructor final. (Far fetched situation, but for completeness)

final private function __construct() {}

Problem

I'm under the interpretation that if I need to access a method statically, I should make the class abstract only if I'll never need it instantiated. Is that true?

Original source