# declaring classes as static or final

- Thread: https://externals.io/message/13047
- Original archive: https://news-web.php.net/php.internals/13047
- Started by Michael Virnstein on 2004-09-28T12:45:40+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13047 — Michael Virnstein — 2004-09-28T12:45:40+00:00

URL: https://externals.io/message/13047#13047

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?

---

## #13048 — Christian Schneider — 2004-09-28T13:02:15+00:00

URL: https://externals.io/message/13047#13048 · In reply to: #13047

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

---

## #13049 — Michael Virnstein — 2004-09-28T13:35:44+00:00

URL: https://externals.io/message/13047#13049 · In reply to: #13048

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 wrote:
> 
> > […]
> 
> 
> PINJ (PHP Is Not Java) ;-)
> 
> Let's keep the language simple, too much was added already IMHO (-:C
> 
> - Chris

---

## #13050 — Michael Virnstein — 2004-09-28T13:44:48+00:00

URL: https://externals.io/message/13047#13050 · In reply to: #13047

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:
> 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?

---

## #13060 — Marcus Boerger — 2004-09-28T21:32:04+00:00

URL: https://externals.io/message/13047#13060 · In reply to: #13050

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.

---

## #13064 — Michael Virnstein — 2004-09-29T09:44:04+00:00

URL: https://externals.io/message/13047#13064 · In reply to: #13060

> > […]
>
> 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.

> > […]
>
> 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

---

## #13067 — Jason Farrell — 2004-09-29T11:51:36+00:00

URL: https://externals.io/message/13047#13067 · In reply to: #13064

Michael Virnstein wrote:

> > […]
>
>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?
>
>
>  
>
> > […]
>
>never said php doesn't support it, i just wanted to give some more input on
>the rules that would fit for my suggestions.
>
>  
>
> > […]
>
>same as above
>
>  
>
> > […]
>time.
>  
>
> > […]
>need
>  
>
> > […]
>have
>  
>
> > […]
>
>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

---

## #13068 — Jochem Maas — 2004-09-29T12:16:33+00:00

URL: https://externals.io/message/13047#13068 · In reply to: #13067

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:
> > […]
> Please see: http://www.php.net/zend-engine-2.php
> This will explain all the new OOP features in PHP5
> 
> Regards,
> Jason

---

## #13069 — Marcus Boerger — 2004-09-29T19:26:40+00:00

URL: https://externals.io/message/13047#13069 · In reply to: #13064

Hello Michael,

Wednesday, September 29, 2004, 11:44:04 AM, you wrote:


> > […]

> 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
