Levi Morrison wrote on 01/12/2014 17:58:
The only thing missing is a standardised, enforced, annotation on the class
to declare that you are using this ability and instances will never be
created.
Let's say a class has no members: what's the harm in instantiating it?
Sure, it logically doesn't do anything useful, but is this user error
such a big deal that we alter the language to disallow it?

The same could be said of many function and class modifiers - they exist as self-documenting, mostly compile-time, assertions. There may not be a compelling reason to add one, but "the user's self-discipline should be good enough" isn't a very compelling reason not to.

I do think the keyword should trigger simple enforcement, though, not automagically change the behaviour.

static class Foo {
    function bar() {
        echo do_something_with($this);
    }
}

Much nicer (and just as easy) for that to give a compile-time error of "Static class Foo cannot contain non-static function bar" than for it to autoload fine, and then give a less obvious error when Foo::bar() is run (probably to do with a non-existent variable, since $this is always undefined inside a static function).

Programmers, and automated tools, won't know about the "static class" annotation immediately, but hopefully already know what "static function" means.

Note that this is the same as with "abstract function" vs "abstract class", only the other way around - adding an abstract function doesn't implicitly convert a class to be abstract, it refuses to compile if you don't explicitly do so.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to