Req #63841 [Com]: Mind the constructor return value
Edit report at https://bugs.php.net/bug.php?id=63841&edit=1
ID: 63841
Comment by: paxcoder+phpnet at gmail dot com
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:
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."
Previous Comments:
------------------------------------------------------------------------
[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)