Req #65103 [Com]: consider the final keyword depracation
Edit report at https://bugs.php.net/bug.php?id=65103&edit=1
ID: 65103
Comment by: knight at kopernet dot org
Reported by: knight at kopernet dot org
Summary: consider the final keyword depracation
Status: Wont fix
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
But the implementation is sealed. The only way to override the construction is to use a factory
method e.g. call your constructor and proceed with remaining initialization in the factory method.
My initialization in the extended class can be as critical as yours yet I cannot do anything to
prevent someone creating an object of my new type with only your(e.g. base) constructor called.
On the other hand if someone creates an object and forgets to call your constructor and it's so
obvious as you say it segfaults than the programmer gets feedback immediately so I don't get
why you insist on preventing it.
But your point is very good. I've seen that hundred times: someone tries to extend a framework
component without truly understanding its lifecycle. A sealed constructor is a good way to make sure
it is used correctly. Better yet if the constructor is protected so that only the factory method can
be used to create the object. That's however imposes the need to adopt the usage of factory
method throughout your codebase.
Previous Comments:
------------------------------------------------------------------------
[2013-06-25 07:45:43] ab@php.net
@knight at kopernet dot org
IMHO that should be either a request to implement something like Reflection
makeInheritable() . The final keyword has its uses and PHP isn't Java. If you're
really in the mood, please consider writing your thoughts about what should be
done as improvement at this place, and discussing it on the internals list. Such a
request is rather useless without discussing all possible pro and contra.
------------------------------------------------------------------------
[2013-06-25 04:36:35] laruence@php.net
I think Final is very useful in the following case:
like I did in Yaf, I got a Yaf class, there are some key resource need to be
initialized. if these resource didn't intialized, then maybe segfault later.
so, I defined a constructor. and do such initialization. but user can extends my
Yaf class, and they also might define there own constructor, and doesn't call the
parent::construct...
so I make the Yaf::construct final.... every is fine now.
------------------------------------------------------------------------
[2013-06-23 09:16:07] knight at kopernet dot org
Description:
------------
The final keyword is widely recognized among many programmers that adopted OOP paradigim esp.
comming from or knowing Java.
I find it very problematic though.
Without a real compilation stage using the final keyword in the type hints does not make sure that
all execution paths in the calling code passes an object of the expected type. This is completely
different from Java which actually makes sure the correctness of the passed arguments.
Also when it comes to writing unit tests - there's no way to override a final class and test
code that makes use or depends on such an object.
Please consider deprecating, removing the final keyword or release the imposed restriction.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65103&edit=1
Thread (4 messages)