php.net |  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
 [2017-09-26 20:38 UTC] 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

Patches

Pull Requests

History

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... (https://3v4l.org/QnvEi)
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] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-09-27 05:22 UTC] requinix@php.net
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 : https://www.php.net/manual/en/language.oop5.decon.php

__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-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC