-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[PHP7] Added support for abstract final classes #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PHP7] Added support for abstract final classes #923
Conversation
Shouldn’t this just be a static class? |
I agree with @lstrojny here, you are right in the sense that abstract + final satisfies your condition (a class which can't be instantiated nor extended) but that's already possible without this change(as you mentioned you can create a final class with a private constructor). |
Conceptually, the "static" operator means that consecutive calls always return same value/instance. This means that at a class level it would return same instance at multiple "new" calls (read as singleton). @Tyrael Parts I already answered previously. This patch is currently missing the enforcement for "static" existence on properties and methods, which I'm almost done. |
Link to the RFC: https://wiki.php.net/rfc/abstract_final_class. |
1a713c6
to
5e90b58
Compare
…me currently defined classes as final which were just not being considered as such before.
@guilhermeblanco SInce the RFC is about static classes now, should this pull be closed? |
@smalyshev I personally don't really care about which one we choose. |
public static function display($name) | ||
{ | ||
echo "Hello $name\n"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation here.
@guilhermeblanco I am compliant with final abstract classes in PHP. In past, I also got this idea in mind but then I just threw it off. |
Feature got rejected. |
Adding a note in case this gets re-proposed later There was a similar question about Java I saw after a brief google search. https://stackoverflow.com/a/9618724 With the introduction of enums in php 8.1, it is now technically possible to create a enum ArrayUtil {
public static function first(array $x) { return reset($x); } // also works on temporaries
// No enum cases are declared.
} Obviously, an ArrayUtil isn't actually an enumeration and this may confuse developers reading the code. Based on the rfc voting results, and the fact that no commonly used language I'm aware of uses "abstract final", |
Consumes the PR #911
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: