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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: saymongcsw at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 06:01:24 2020 UTC