php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65930 new does not evaluate arguments if class has no constructor
Submitted: 2013-10-18 16:19 UTC Modified: 2013-10-22 12:02 UTC
From: php at mlemoine dot name Assigned:
Status: Duplicate Package: Documentation problem
PHP Version: 5.5.5 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at mlemoine dot name
New email:
PHP Version: OS:

 

 [2013-10-18 16:19 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-22 11:55 UTC] 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-22 12:02 UTC] bwoebi@php.net
-Status: Open +Status: Duplicate
 [2013-10-22 12:02 UTC] bwoebi@php.net
Duplicate of https://bugs.php.net/bug.php?id=54170
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC