Abstract final classes / Static classes View as Markdown

Declined
Authors
Guilherme Blanco, guilhermeblanco@php.net
Date
2014-11-26
Version
1.1
Source
wiki.php.net/rfc/abstract_final_class

Vote

Static classes closed
Yes, as "static class"
5
Yes, as "abstract final class"
0
No
12
17 voters
ab No 2014-12-20 10:22
ajf No 2014-12-12 16:13
bwoebi No 2014-12-13 01:29
derick No 2014-12-13 19:17
dm No 2014-12-15 08:58
dragoonis No 2014-12-14 16:33
guilhermeblanco Yes, as "static class" 2014-12-12 15:41
hywan No 2014-12-20 10:21
jedibc No 2014-12-15 09:04
jpauli Yes, as "static class" 2014-12-19 10:38
kalle Yes, as "static class" 2014-12-12 17:49
kinncj Yes, as "static class" 2014-12-12 16:29
krakjoe No 2014-12-12 16:15
levim No 2014-12-12 17:58
malukenho Yes, as "static class" 2014-12-13 19:13
sebastian No 2014-12-16 08:12
stas No 2014-12-12 18:29

Discussions on the mailing list

[RFC] Abstract final classes
· 15 messages
[RFC] Static classes (Was Abstract final classes)
by Robert Stoll · 20 messages
[VOTE] Abstract final / Static classes
· 10 messages
Idea: Function autoloading using dummy namespaces
by Rowan Collins · 11 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

Please understand only one of the following proposals can be accepted, not both. They're explained together because the address the same issue.

As Static classes

Static classes are helpful in the case you are wrapping common functions that are static, but the common class itself cannot be instantiated. Currently, PHP developers' only resource is to create a final class with a private constructor, leading to untestable and error prone code.

When untestable/error prone code is mentioned is that there's no signaling of broken definition by creating a regular class with private constructor and other members are not declared as static. Everything will just compile smoothly (file inclusion/autoloading).

By the acceptance of this patch, every method declaration would automatically be enforced to behave as static without any further keyword needed. This reduces the human error of methods being forgotten to be declared as static.

For such, here is motivation:

  • As “static”, it purely behaves at class level, not at instance level
  • Could also be used with combination of “final”, meaning it cannot be extended (such as more permissive visibility, behavior change, etc)
  • There's no way of adding static variables to a namespace. This would address this issue too.

As abstract final

Abstract final classes are helpful in the case you are wrapping common functions that are static, but the common class itself cannot be instantiated. Currently, PHP developers' only resource is to create a final class with a private constructor, leading to untestable and error prone code.

For such, here is motivation:

  As “abstract”, it cannot be instantiated
  As “final”, it cannot be extended (such as visibility increase, behavior change, etc)
  There's no way of adding variables to a namespace. This would address this issue too

Proposal

Change language scanner to accept static class or abstract final construction, and subsequently enforcing the existence of only static members.

Proposed PHP Version(s)

PHP 7

Example

As static class

static class Environment
{
    private static $rootDirectory = '/var/www/project';
    public static function getRootDirectory()
    {
        return self::$rootDirectory;
    }
}
echo Environment::getRootDirectory();

As abstract final

abstract final class StringUtil
{
    public static function isNotNullOrSpace($str)
    {
        return !($str === null || strlen($str) < 1 || strlen(trim($str)) < 1);
    }
}
echo StringUtil::isNotNullOrSpace('')
  ? 'Empty string'
  : 'Non-empty string';

Vote

Vote: Static classes — Yes, as "static class": 5 · Yes, as "abstract final class": 0 · No: 12 (closed)

The vote starts on 12/12/2014 and ends on 12/19/2014. 2/3 majority required.

Implementation