Req #75263 [Com]: function __construct(): void

From: Date: Wed, 03 Jan 2018 16:45:24 +0000
Subject: Req #75263 [Com]: function __construct(): void
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213364@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75263&edit=1 ID: 75263 Comment by: Andy_Schmidt at HM-Software dot com Reported by: saymongcsw at gmail dot com Summary: function __construct(): void Status: Not a bug Type: Feature/Change Request Package: Class/Object related Operating System: any PHP Version: 7.1.10RC1 Block user comment: N Private report: N New Comment: Since the constructor must use return 'somevalue'; to return a value, I consequently do believe that declaring: function __construct(): void { ... } would actually be the proper way to document that restriction - now that "void" is a valid function return type? Previous Comments: ------------------------------------------------------------------------ [2017-09-27 05:22:41] requinix@php.net Constructors in object-oriented languages do not have return types. ------------------------------------------------------------------------ [2017-09-26 22:54:51] spam2 at rhsoft dot net but to gain what? it's a magic function with special behavior anyways (see new my_class(1, 2) which calls __constrcut without naming it) ------------------------------------------------------------------------ [2017-09-26 21:48:50] saymongcsw at gmail dot com I mean add possibility for :void in constructor without behavior change. Constructor would be good for configure object but without constructors in PHP works too... (https://3v4l.org/QnvEi) also see link in comment above that show in hhvm-3.21.0 works as expected. ------------------------------------------------------------------------ [2017-09-26 21:29:34] spam2 at rhsoft dot net why do you think a void retunr-type would be correct for __construct() $x = new my_class(1, 2) call __construct(1, 2) of my_class and returns a object instance ------------------------------------------------------------------------ [2017-09-26 20:38:36] saymongcsw at gmail dot com Description: ------------ Can't declare VOID return type in constructor. (ex. https://3v4l.org/9slJl) Test script: --------------- <?php class Say { public function __construct(): void { } public function hello(): string { return 'Hello'; } } echo (new Say)->hello(); Expected result: ---------------- Hello Actual result: -------------- Fatal error: Constructor Say::__construct() cannot declare a return type in %s on line 3 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75263&edit=1

« previous php.bugs (#213364) next »