-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Allow void return type on constructors/destructors #5717
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
Conversation
Let me know if additional tests are necessary (e. g. type widening). |
As I've said before, I would prefer to just implicitly enforce |
@nikic There is a separate pull request for patching that bug and enforcing |
@moliata A trailing comma is helpful to avoid modifying multiple lines when adding/removing items. I'm not sure what the benefit of explicitly writing void on constructors is, though. It's needless boilerplate that will just lead to code style arguments. I'm not even sure it makes sense, since constructors implicitly return a new instance of the class. |
Thank you @theodorejb for your opinion. Here is my take on it and my reasons as to why we should allow
<?php
class Test {
public function __construct() {}
}
class Test2 extends Test {
public function __construct() {
// WTF? Why can't I do this? No return type means mixed|void, right?
$parent = parent::__construct();
}
}
<?php
...
// PHP manual explicitly states (in multiple pages) void return type
public function __construct(): void;
<?php
...
$object = new Test();
// Also implicitly returns a cloned object yet after Gabriel's PR
// we will be able to use void return type on __clone
$object_2 = clone $object;
|
@moliata Most other magic methods that allow a return type allow more types than |
I'm closing this PR since people dislike it so much, that someone can go as far as to email me a request to commit suicide. |
That's totally inappropriate. I respect you trying to help the community with what you think it's good. Thank you! |
First of all, I am terribly sorry you have had this experience. I highly advice you to send an email to While you may feel discouraged to continue on the RFC, and respectfully so. I would encourage you to try look past this behavior as a bump in the road and continue with the RFC. On a personal note, I think with @carusogabriel's current RFC, something like this is definitely an avenue to explore and something I could see myself write as a style, given it is a finger habbit to type a return type statement after each method you write. Thank you, |
Thank you @KalleZ and @drealecs! No offense was taken regarding the suicide email, had a good laugh instead tbh:). I closed the RFC since it seemed that just about everyone didn't really want to allow void return type on constructors and didn't want to start code style wars. I do have a vision of having perfect consistency but some might not, so it seemed it wasn't worth the time and effort to put this RFC into voting, given that there weren't a lot of people supporting the idea. |
@moliata I am glad to hear that. Regarding the RFC, I assume it would be an optional return type, therefore it is a more or less cosmetic addition. Those disagreeing it with simply do not have to use it, those who write cross version compatible code probably won't use it for a while anyway which will probably cover most of those nay-sayers. Only rarely you can ever come to a unanimous agreement (if ever) regarding style, but optional additions to for more verbose styles should always be welcomed (similar to the trailing commas RFCs). If it is something you personally desire to have added to PHP, I still highly encourage you to pursue it. |
So after some consideration, I am reopening the PR and will push the RFC under discussion in the upcoming days. I'll try to emphasize a bit more that this change is optional. Thanks everybody:) |
Hi @moliata , did you get inspired by my bug report or even my PR #5678 ? Do you think you it is good idea to disallow explicit In other methods (destructor etc.) I think allowing |
Hey @mvorisek, Since both PHP core members and r/PHP redditors are mostly okay with this change, I'm not planning to change my RFC: allow void return type on both constructors and destructors. By suggestion, what I'm proposing is to join forces :) and incorporate these optional, cosmetic-only additions as well as the validation into a single RFC. If that's okay with you, hit me up at my public email account. Best regards, |
Closing this PR and making a new one since the updated RFC will not only allow optional explicit |
TODO list: