DeveloperJoy Logo DeveloperJoy

Static Classes in PHP: A New RFC Proposal

June 27, 2024
Static Classes in PHP: A New RFC Proposal

The PHP community is currently discussing an important new feature: static classes.

This proposal, outlined in the RFC by Paul Morris, introduces the concept of a static class, which is a class that cannot be instantiated and whose members are all static.

The proposal aims to incorporate this feature in PHP version 8.4, or in the next one. The RFC is currently under discussion, and its adoption could bring significant changes to how developers express intent and ensure class behaviors at compile-time.

Understanding Static Classes

A static class in PHP, as proposed, is a class marked with the static keyword. This marking means the class cannot be instantiated, and all its properties and methods must be static. The proposal highlights several benefits of explicitly marking a class as static:

  1. Clear Developer Intent: Explicitly marking a class as static makes the developer's intention clear, reducing ambiguity.
  2. Compile-Time Checks: The PHP engine can perform compile-time checks to ensure no instance members are included.
  3. Runtime Checks: The engine can also enforce runtime checks to prevent any instantiation attempts.

Key Features of the Proposal

The proposal includes several specific features and rules for static classes:

  1. Static Keyword: The static keyword is used at the class level.
    static class Foo {}
  2. Instantiation Prohibited: Any attempt to instantiate a static class (e.g., using new, ReflectionClass::newInstance*, or unserialize) will result in a runtime error.
  3. Static Properties and Methods: All members of a static class must be static.
  4. Inheritance: A static class can be marked as final, but if it is not, it can still be extended. Static classes may extend other static or non-static classes provided they do not contain instance members.
  5. Traits: Static classes can use traits, allowing for reusable code.
  6. Magic Methods: Only static magic methods, such as __callStatic(), are supported.

Design Considerations

The proposal raises several questions and design considerations:

  1. Final Modifier: Should a static class imply final?
  2. Inheritance Rules: Should a static class prohibit inheritance, similar to C#?
  3. State Management: Should a static class permit state management through static properties?
  4. Trait Usage: Should traits be allowed in static classes?
  5. Magic Methods: Which magic methods should be supported?

Implementation Details

The proposal suggests that the static keyword should still be required for member declarations within a static class, despite the class itself being marked as static. This decision is to ensure clarity throughout the class body, especially for developers jumping into different parts of the class.

Compatibility and Future Scope

The proposal asserts that there are no known backward-incompatible changes associated with introducing static classes. The feature is considered complete by itself, but there is room for future enhancements, such as supporting read-only static properties if technical limitations are overcome.

Community Involvement

The community is invited to discuss and provide feedback on the proposal. A draft pull request by Lanre is available for review, and the proposal includes references to similar features in other programming languages, like C#.

The introduction of static classes in PHP aims to bring more robust and explicit design patterns to the language, providing clear developer intent and stringent compile-time and runtime checks.

As the PHP community discusses this RFC, the potential adoption in PHP 8.4 could mark a significant step forward in PHP's evolution, aligning it more closely with features available in other modern programming languages.

For more details and to participate in the discussion, you can view the full RFC.

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995


What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995


Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.