php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78313 Return from constructor should trigger an error or warning
Submitted: 2019-07-19 17:24 UTC Modified: 2020-12-18 16:29 UTC
From: php-bugs at allenjb dot me dot uk Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: Next major version OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php-bugs at allenjb dot me dot uk
New email:
PHP Version: OS:

 

 [2019-07-19 17:24 UTC] php-bugs at allenjb dot me dot uk
Description:
------------
It's currently possible to write a constructor that returns a value, but there is no way (as far as I can tell) to retrieve this value when constructing an object. Technically you can call the constructor directly, but this is not normally done in the course of object construction (other than calling the parent constructor).

While to experienced developers, the fact that you should/cannot return a value from a constructor seems obvious, this is not always so to newbies - https://old.reddit.com/r/webdev/comments/cf9l3h/working_php_code_working_on_a_test_server_isnt/

While thorough testing would obviously reveal this issue in the above case, we all know that not everyone, especially newbies, thoroughly test their code. In the above case the issue isn't obvious until an error in the curl query occurs, but all the user ends up with is an improperly constructed object - even if their code at the point of construction checks for a string value rather than an object, this check will never trigger.

Attempting to declare a return type already triggers an error: https://3v4l.org/rsikK

Test script:
---------------
<?php

class Foo
{
    
    public function __construct()
    {
        return "bar";
    }
}

$foo = new Foo();
var_dump($foo);

Expected result:
----------------
Error / warning that constructors cannot return a value

Actual result:
--------------
object(Foo)#1 (0) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-19 18:03 UTC] girgias@php.net
-Summary: return from constructor should trigger an error or warning +Summary: Return from constructor should trigger an error or warning -Operating System: n/a +Operating System: -PHP Version: 7.3.7 +PHP Version: Next major version
 [2019-07-19 18:03 UTC] girgias@php.net
I think there was some talk/PR to do something about calling magic methods directly which includes the constructor, destructor, __clone(), __isset() and other.

I do agree that it should do something but that would probably need an RFC from my understanding.
 [2020-12-18 16:29 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-12-18 16:29 UTC] cmb@php.net
There is a declined RFC[1] regarding enforcing void semantics.  If
you strongly feel that this decision should be reviewed, please
forward the request to the internals mailing list, and prepare to
go through the RFC process[2].

For now, you can manually declare the constructor void to catch
inadvertent returns: <https://3v4l.org/G00Yc>.

[1] <https://wiki.php.net/rfc/make_ctor_ret_void>
[2] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC