Req #78313 [NEW]: return from constructor should trigger an error or warning

From: Date: Fri, 19 Jul 2019 17:24:55 +0000
Subject: Req #78313 [NEW]: return from constructor should trigger an error or warning
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-221860@lists.php.net to get a copy of this message
From:             php-bugs at allenjb dot me dot uk
Operating system: n/a
PHP version:      7.3.7
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:return from constructor should trigger an error or warning

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) {
}

-- 
Edit bug report at https://bugs.php.net/bug.php?id=78313&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=78313&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=78313&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=78313&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=78313&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=78313&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=78313&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=78313&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=78313&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=78313&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=78313&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=78313&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=78313&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=78313&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78313&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=78313&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=78313&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=78313&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78313&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=78313&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=78313&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#221860) next »