Doc #76311 [Ver->]: __construct/destruct/clone do not have return types
| From: | Andy_Schmidt at HM-Software dot com | Date: | Tue, 08 May 2018 17:05:16 +0000 |
| Subject: | Doc #76311 [Ver->]: __construct/destruct/clone do not have return types | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15662@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76311&edit=1
ID: 76311
User updated by: Andy_Schmidt at HM-Software dot com
Reported by: Andy_Schmidt at HM-Software dot com
Summary: __construct/destruct/clone do not have return types
-Status: Verified
+Status: Verifiedo
Type: Documentation Problem
Package: Class/Object related
PHP Version: 7.2.5
Block user comment: N
Private report: N
New Comment:
I understand that some methods had always implicitly disallowed return values before there had been
a syntax to do so explicitly. The hard syntax error certainly was proper before 7.1.
However, this is 2018. Why would we still force occasional PHP users from having to memorize the
signature for each and every magic method?
Now that "void" is a type hint, it seems that an author writing self-documenting code (for
the benefit of less-versed coworkers) should not be penalized with run-time errors for explicitly
defining VOID?
I respectfully suggest that with 7.1 and higher, the message category
"... cannot declare a return type"
should simply recognize the now-available type "void" as being a perfectly expression of
the implicit rule!
Previous Comments:
------------------------------------------------------------------------
[2018-05-08 16:49:06] requinix@php.net
Related To: Bug #76312
------------------------------------------------------------------------
[2018-05-08 16:47:16] requinix@php.net
True, but __wakeup allows for a void return type. Which I don't mind because its counterpart
__sleep does have a return type.
To me it's more the fact that __construct/destruct are particularly special methods and
explicitly disallow return types. __clone too, now that I look at the list.
------------------------------------------------------------------------
[2018-05-08 16:39:53] cmb@php.net
> Constructors and destructors do not have the concept of a return
> type.
The same is true for some of the other magic methods (e.g.
__wakeup()).
------------------------------------------------------------------------
[2018-05-08 15:50:26] requinix@php.net
Constructors and destructors do not have the concept of a return type. The docs showing anything at
all, even void, is incorrect.
------------------------------------------------------------------------
[2018-05-08 15:37:05] Andy_Schmidt at HM-Software dot com
Description:
------------
According to PHP's manual:
http://php.net/manual/en/language.oop5.decon.php
the signature of the __destruct method is:
void __destruct ( void )
Consequently, the following syntax would be correct for 7.2, now that a "void" type hint
is available:
function __destruct(): void {}
Actual result:
--------------
PHP Fatal error: Destructor __destruct() cannot declare a return type
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76311&edit=1