Skip to content

[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

Closed

Conversation

guilhermeblanco
Copy link

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:

  • As "abstract", it cannot be instantiated
  • As "final", it cannot be extended (such as visibility increase, behavior change, etc)
  • There's no way of adding variables to a namespace. This would address this issue too

@lstrojny
Copy link
Contributor

Shouldn’t this just be a static class?

@Tyrael
Copy link
Contributor

Tyrael commented Nov 26, 2014

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).
Also, I fail to see how would this be a better alternative (you mentioned testability for example, as far as I can tell final + abstract would be equally untestable as final+private constructor) plus it would be a bit stretching the definition of the abstract modifier, as that is about a contract/requirement to the subclasses.
Using that together with final which explicitly prohibits the existence of subclasses is confusing/conflicting idea and I can't even remember a single language which supports that combination(from the top of my head).

@guilhermeblanco
Copy link
Author

@lstrojny Shouldn’t this just be a static class?

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.
Now there is a language that does that... Hack. =) Here is the commit that introduces this support, which I happened to be pointed out by a friend today: facebook/hhvm@faedfaf
Also, C# supports the same configuration. I added the same enforcement both Hack and C# do (must be static members, covering both properties and methods).

@Hywan
Copy link
Contributor

Hywan commented Nov 27, 2014

Link to the RFC: https://wiki.php.net/rfc/abstract_final_class.

Guilherme Blanco added 3 commits December 1, 2014 16:36
@smalyshev
Copy link
Contributor

@guilhermeblanco SInce the RFC is about static classes now, should this pull be closed?

@guilhermeblanco
Copy link
Author

@smalyshev I personally don't really care about which one we choose.
My original idea was to ask about "abstract final", matching HHVM implementation, but also consider C# "static class" too. Maybe we should leave both open, ask for 3 options to vote (static class, abstract final class or no support) and then close/merge whatever is appropriate.
What do you think?
We could have 2 questions. One is yay/nay for such of support and if positive, and the other to choose between the two approaches.

public static function display($name)
{
echo "Hello $name\n";
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation here.

@jpauli jpauli added the PHP7 label Dec 12, 2014
@Kubo2
Copy link
Contributor

Kubo2 commented Dec 14, 2014

@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.

@Kubo2 Kubo2 mentioned this pull request Dec 14, 2014
@guilhermeblanco
Copy link
Author

Feature got rejected.

@TysonAndre
Copy link
Contributor

TysonAndre commented Apr 7, 2021

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
collection of static methods that can't be instantiated - see https://wiki.php.net/rfc/enumerations

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.
A static analyzer or linter would be capable of warning about instance properties or instance methods on enums with no cases to somewhat prevent accidental misuse.

Based on the rfc voting results, and the fact that no commonly used language I'm aware of uses "abstract final",
I personally doubt subsequent attempts at introducing either option would achieve a 2/3 majority https://wiki.php.net/rfc/abstract_final_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants