Doc #65930 [Opn->Dup]: new does not evaluate arguments if class has no constructor
| From: | bwoebi@php.net | Date: | Tue, 22 Oct 2013 12:02:09 +0000 |
| Subject: | Doc #65930 [Opn->Dup]: new does not evaluate arguments if class has no constructor | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10521@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65930&edit=1
ID: 65930
Updated by: bwoebi@php.net
Reported by: php at mlemoine dot name
Summary: new does not evaluate arguments if class has no
constructor
-Status: Open
+Status: Duplicate
Type: Documentation Problem
Package: Documentation problem
Operating System: Irrelevant
PHP Version: 5.5.5
Block user comment: N
Private report: N
New Comment:
Duplicate of https://bugs.php.net/bug.php?id=54170
Previous Comments:
------------------------------------------------------------------------
[2013-10-22 11:55:39] daverandom@php.net
Note that the test script is incorrect, it should be ++$this->i. When this change is effected,
the reported problem can be seen.
I personally would consider this a bug with PHP and not with the documentation, as the arguments
should, in my view, always be evaluated regardless of whether the class has a constructor.
------------------------------------------------------------------------
[2013-10-18 16:19:57] php at mlemoine dot name
Description:
------------
Either new \MyClass($foo->getBar()) is used or new $class($foo->getBar()) is used. If the
instanciated class has no constructor explicitly defined or inherited, $foo->getBar() will never
be invoked. This prevent any side-effect to be run and can produce very unexpected behaviour.
This should either be fixed so the arguments are ALWAYS evaluated, or documented as such.
CF example belows
Test script:
---------------
<?php
class foo { public $i = 0; function createANewBar() { ++$i; return null; } }
class no_construct { }
class a_construct { public function __construct() { } }
$myFoo = new foo;
echo $myFoo->i.PHP_EOL;
$class = 'no_construct';
$object = new $class($myFoo->createANewBar());
echo $myFoo->i.PHP_EOL;
$object = new no_construct($myFoo->createANewBar());
echo $myFoo->i.PHP_EOL;
$class = 'a_construct';
$object = new $class($myFoo->createANewBar());
echo $myFoo->i.PHP_EOL;
$object = new a_construct($myFoo->createANewBar());
echo $myFoo->i.PHP_EOL;
Expected result:
----------------
0
1
2
3
4
Actual result:
--------------
0
0
0
1
2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65930&edit=1