php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79679 non-void return in __construct should generate at least warning
Submitted: 2020-06-06 14:38 UTC Modified: 2020-06-07 10:04 UTC
From: michael dot vorisek at email dot cz Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.4.6 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
50 - 25 = ?
Subscribe to this entry?

 
 [2020-06-06 14:38 UTC] michael dot vorisek at email dot cz
Description:
------------
__construct is special methods and it should be not allowed to return any value

Test script:
---------------
class A {
    public function __construct() {
        return 5;
    }
}

class B extends A {
    public function __construct() {
        var_dump(parent::__construct());
    }
}

$b = new B();

Expected result:
----------------
PHP warning

Actual result:
--------------
int(5)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-06 19:35 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2020-06-06 19:35 UTC] requinix@php.net
While likely to be uncontroversial given the passing of the magic methods signature checks, this sort of thing needs to be discussed on the internals list.
https://wiki.php.net/rfc/magic-methods-signature
https://www.php.net/mailing-lists.php

Note that __destruct is also affected, as well as possibly __clone, __set, __unserialize, __unset, and __wakeup.
 [2020-06-07 10:04 UTC] cmb@php.net
Just add a `void` return type hint to __construct().
 [2020-06-07 10:11 UTC] bugreports at gmail dot com
> Just add a `void` return type hint to __construct()

the nonesense beahvior when doing so is what that topic is all about when the docs say  __construct ([ mixed $args = "" [, $... ]] ) : void" and you get "Fatal error: Constructor x::__construct() cannot declare a return type" by declare void as return type
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC