====== PHP RFC: Optional Constructor body ====== * Version: 0.9 * Date: 2024-07-18 * Author: Oliver Nybroe, olivernybroe@gmail.com * Status: Draft (or Under Discussion or Accepted or Declined) * First Published at: https://wiki.php.net/rfc/optional_constructor_body ===== Introduction ===== This RFC proposes the optional of having constructors without a body. With the introduction or constructor property promotion in 8.0, we often see classes where the constructor has an empty body, and my guess would be that this will only increase with the introduction of property access hooks in 8.4 which is allowed to be defined in the constructor also. This means most code ends up with a constructor that has `{}` at the end, to create an empty body. The readability of it is not very nice and from many coding styles this ends up being split into multiple lines. ``` class User { public function __construct( private string $name, ) { } } ``` This RFC proposed a short syntax which is purely a coding style choice as an alternative ``` class User { public function __construct( private string $name, ); } ``` ===== Proposal ===== All the features and examples of the proposal. To [[http://news.php.net/php.internals/66051|paraphrase Zeev Suraski]], explain hows the proposal brings substantial value to be considered for inclusion in one of the world's most popular programming languages. Remember that the RFC contents should be easily reusable in the PHP Documentation. If applicable, you may wish to use the language specification as a reference. ===== Backward Incompatible Changes ===== What breaks, and what is the justification for it? ===== Proposed PHP Version(s) ===== PHP 8.4 ===== RFC Impact ===== ==== To SAPIs ==== Describe the impact to CLI, Development web server, embedded PHP etc. ==== To Existing Extensions ==== Will existing extensions be affected? ==== To Opcache ==== It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. Please explain how you have verified your RFC's compatibility with opcache. ===== Open Issues ===== Make sure there are no open issues when the vote starts! ===== Unaffected PHP Functionality ===== List existing areas/features of PHP that will not be changed by the RFC. This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. ===== Future Scope ===== As this RFC is following the same syntax for definitions with no bodies as interfaces uses, future proposals could be centered around making body optional in other definitions. - Making all methods have an optional body. Could be useful for when implementing a method from an interface where your implementation should not do anything, or for changing signature on a method without touching the logic. ```php class User implements Deletable { public function delete(); } ``` - Making classes have an optional body Having no body on a class would be very useful for custom exceptions as often, no body is needed for them. ``` class DaemonNotFoundException extends RuntimeException; ``` This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC. ===== Proposed Voting Choices ===== Include these so readers know where you are heading and can discuss the proposed voting options. ===== Patches and Tests ===== Links to any external patches and tests go here. If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. Make it clear if the patch is intended to be the final patch, or is just a prototype. For changes affecting the core language, you should also provide a patch for the language specification. ===== Implementation ===== After the project is implemented, this section should contain - the version(s) it was merged into - a link to the git commit(s) - a link to the PHP manual entry for the feature - a link to the language specification section (if any) ===== References ===== Links to external references, discussions or RFCs ===== Rejected Features ===== Keep this updated with features that were discussed on the mail lists.