Req #63841 [Nab]: Mind the constructor return value

From: Date: Wed, 02 Jan 2013 11:13:02 +0000
Subject: Req #63841 [Nab]: Mind the constructor return value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175695@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63841&edit=1

 ID:                 63841
 Updated by:         krakjoe@php.net
 Reported by:        paxcoder+phpnet at gmail dot com
 Summary:            Mind the constructor return value
 Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Class/Object related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

No you're code doesn't have bugs, or unexpected behavior. It is simply wrong.

As I said, a constructor's job is not to return a value but to construct an 
object: To implement a check to ensure it is being used correctly would cost 
every object that PHP ever constructs and would be detrimental and pointless.

Simply revise your code.


Previous Comments:
------------------------------------------------------------------------
[2012-12-31 01:41:46] paxcoder+phpnet at gmail dot com

The check is useful to the programmer, and it should be possible to preform it 
statically.

As for the alternative - enabling returning arbitrary values - it could be 
convenient as well. However, I agree that it is unorthodox.

> Not a bug.

"My code doesn't have bugs, it has unexpected features."

------------------------------------------------------------------------
[2012-12-30 15:45:45] krakjoe@php.net

The job of a constructor is not to return a value but to construct an object, 
this is implicit in the implementation and associated keywords.

For example, one cannot call $var=stdClass::__construct(), because they are not, 
and cannot be static functions.

Making such a check is completely unnecessary and costly.

Not a bug.

------------------------------------------------------------------------
[2012-12-23 16:20:35] paxcoder+phpnet at gmail dot com

Description:
------------
As-is, returning values in the constructor has no effect, and the class instance 
is returned instead. Either allow for explicitly returned values to supersede the 
object, or warn about trying to return a value from a constructor.

Test script:
---------------
<?php
error_reporting(E_ALL|E_STRICT);
class C
{
    function __construct()
    {
        return NULL;
    }
}
$c = new C;
print_r($c);
?>


Expected result:
----------------
Either a warning at line 10, or $c set to NULL

Actual result:
--------------
No warning, and $c set to a C Object


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63841&edit=1


Thread (5 messages)

« previous php.bugs (#175695) next »