|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75263 function __construct(): void
Submitted: 2017-09-26 20:38 UTC Modified: 2017-09-27 05:22 UTC
From: saymongcsw at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.1.10RC1 OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: saymongcsw at gmail dot com
New email:
PHP Version: OS:


 [2017-09-26 20:38 UTC] saymongcsw at gmail dot com
Can't declare VOID return type in constructor. (ex.

Test script:

class Say
    public function __construct(): void

    public function hello(): string
        return 'Hello';

echo (new Say)->hello();

Expected result:

Actual result:
Fatal error: Constructor Say::__construct() cannot declare a return type in %s on line 3


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-26 21:29 UTC] 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 21:48 UTC] 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... (
also see link in comment above that show in hhvm-3.21.0 works as expected.
 [2017-09-26 22:54 UTC] 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-27 05:22 UTC]
-Status: Open +Status: Not a bug
 [2017-09-27 05:22 UTC]
Constructors in object-oriented languages do not have return types.
 [2018-01-03 16:45 UTC] Andy_Schmidt at HM-Software dot com
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?
 [2019-06-26 05:10 UTC] elliseprod at gmail dot com
Actually, watch the PHP doc :

__construct ([ mixed $args = "" [, $... ]] ) : void

So, I agree with having the opportunity to declare VOID return type in constructor
 [2021-04-20 10:39 UTC] somemail at gmail dot com
What is the point of throwing a fatal error when declaring the correct void return type for __construct() and __destruct() and __clone() ?
Those functions do not return a data type, but they DO return to the place from where they have been called, don't they?

And if a fatal error should be throwed, then why isn't there a fatal error for other magic methods, like __unserialize() and __wakeup() ?
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Dec 06 00:01:28 2023 UTC