declaring classes as static or final

php.internals

Michael Virnstein

21 years ago
Hi Devs, what would probably be a nice addition: 1. static classes: static classes can only contain static methods and attributes <?php static class foo { public static function bar() {} } ?> 2. final classes: final classes can't be extended final class foo { public function bar() {} } 3. final static classes final static classes can only contain static methods and attributes and can't be extended. final static class foo { public static function bar() {} } What do you think about it?

Christian Schneider

21 years ago
Michael Virnstein wrote:
> What do you think about it?
PINJ (PHP Is Not Java) ;-) Let's keep the language simple, too much was added already IMHO (-:C - Chris

Michael Virnstein

21 years ago
i don't any experience with java, but i ran into a case, where this would have been useful. And i disagree with the "too much was added already". I think that php, dispite all the added stuff, is still easy to learn, easy to read and easy to work with. PHP simply got more powerful and i hope it'll grow further. Adding the things i mentioned would simply mean more control on the implementation of a class. It wouldn't get more complicated in any way and there'd be noone forcing you to use those things. You would simply have tools to enforce some rules, which is not a bad thing. Michael Christian Schneider wrote:

Michael Virnstein

21 years ago
things i'd like to add: 4. abstract classes can be defined static also <?php abstract static class foo { abstract public static function bar(); } this means that all subclasses of foo must also be static and can only contain static methods and attributes 5. abstract classes can't be defined final Regards, Michael Michael Virnstein wrote:

Marcus Börger

21 years ago
Hello Michael, Tuesday, September 28, 2004, 3:44:48 PM, you wrote:
> things i'd like to add:
> 2. final classes: > final classes can't be extended
already supported
> 4. abstract classes can be defined static also
makes no sense (duplicate): both mean you cannot instantiate the class.
> <?php > abstract static class foo { > abstract public static function bar(); > }
abstract static methods are already supported
> this means that all subclasses of foo must also be static and can only > contain static methods and attributes
> 5. abstract classes can't be defined final
already supported static classes would be implementable at the cost of increasing compiler time. To decide whether to think more about it we'd to know a case where you need such syntax, what you win by having it and what you loose if you don't have it. regards marcus p.s.: as a hint, next time read the docs before requesting syntax we support already.

Michael Virnstein

21 years ago
> > 2. final classes: > > final classes can't be extended > > already supported
Cannot find it in the docs. http://www.php.net/manual/en/language.oop5.final.php doesn't seem to have any comments on that. I'm not at home at the moment, so i couldn't try, but are you really sure that final CLASSES are supported? You're not confusing it with final METHODS, right?
> abstract static methods are already supported
never said php doesn't support it, i just wanted to give some more input on the rules that would fit for my suggestions.
> > 5. abstract classes can't be defined final > > already supported
same as above
> > static classes would be implementable at the cost of increasing compiler
time.
> To decide whether to think more about it we'd to know a case where you
need
> such syntax, what you win by having it and what you loose if you don't
have
> it.
A good example for a static class imo, would be e.g. PEAR's DB Class. The DB-Class itself doesn't need to be instantiate, it simply works as factory and has some functions to check stuff. that'd be a class that i would define as "final static class DB {}". The class should not be extended and should not containt non static methods or attributes. Sure it isn't a big deal, if ppl are instantiating the DB Class, but it only has static methods and instantiating it wouldn't make any sense. Regards, Michael

Jason Farrell

21 years ago
Michael Virnstein wrote:
>>>2. final classes: >>>final classes can't be extended >>> >>> >>already supported >> >> > >Cannot find it in the docs. >http://www.php.net/manual/en/language.oop5.final.php doesn't seem to have >any comments on that. >I'm not at home at the moment, so i couldn't try, but are you really sure >that final CLASSES are supported? >You're not confusing it with final METHODS, right? > > > > >>abstract static methods are already supported >> >> > >never said php doesn't support it, i just wanted to give some more input on >the rules that would fit for my suggestions. > > > >>>5. abstract classes can't be defined final >>> >>> >>already supported >> >> > >same as above > > > >>static classes would be implementable at the cost of increasing compiler >> >> >time. > > >>To decide whether to think more about it we'd to know a case where you >> >> >need > > >>such syntax, what you win by having it and what you loose if you don't >> >> >have > > >>it. >> >> > >A good example for a static class imo, would be e.g. PEAR's DB Class. The >DB-Class itself doesn't need to be instantiate, >it simply works as factory and has some functions to check stuff. that'd be >a class that i would define as >"final static class DB {}". The class should not be extended and should not >containt non static methods or attributes. Sure it isn't >a big deal, if ppl are instantiating the DB Class, but it only has static >methods and instantiating it wouldn't make any sense. > >Regards, Michael > > >
Please see: http://www.php.net/zend-engine-2.php This will explain all the new OOP features in PHP5 Regards, Jason

Jochem Maas

21 years ago
it seems to me that the documentation pointed to is a little out of date, at least the part about reflection is using 'old' (read: notStudlyCapsCS) class names - might be worth fixing that to avoid a hundred-thousand emails along the lines of 'reflection is broken'. kind regards, Jochem ps - I just discovered the profiling possibilities of XDEBUG, nice one Derick (& co?)! Jason Farrell wrote:

Marcus Börger

21 years ago
Hello Michael, Wednesday, September 29, 2004, 11:44:04 AM, you wrote:
>> > 2. final classes: >> > final classes can't be extended >> >> already supported
> Cannot find it in the docs. > http://www.php.net/manual/en/language.oop5.final.php doesn't seem to have > any comments on that. > I'm not at home at the moment, so i couldn't try, but are you really sure > that final CLASSES are supported? > You're not confusing it with final METHODS, right?
Nope, i invented both final and abstract for both classes and methods :-)
> A good example for a static class imo, would be e.g. PEAR's DB Class. The > DB-Class itself doesn't need to be instantiate, > it simply works as factory and has some functions to check stuff. that'd be > a class that i would define as > "final static class DB {}". The class should not be extended and should not > containt non static methods or attributes. Sure it isn't > a big deal, if ppl are instantiating the DB Class, but it only has static > methods and instantiating it wouldn't make any sense.
Ah so it is just a little bit more enforcement then the following: final class DB { // prevent instantiation private function __construct() { exit(0); } // The following is not needed because the constructor cannot be called // private function __clone() { exit(0); } }
-- Best regards, Marcus mailto:helly@php.net

Michael Virnstein

21 years ago
> Ah so it is just a little bit more enforcement then the following: > > final class DB > { > // prevent instantiation > private function __construct() { exit(0); } > > // The following is not needed because the constructor cannot be
called
> // private function __clone() { exit(0); } > }
it's almost the same thing, but personally, i'd find it much more readable and comprehensible than the code above. "final static class" is easy to understand: Class can't be extended, can't be instantiated and can't contain any non-static methods and attributes. helly@php.net wrote: