Bug #66577 [Opn]: Variable assignment in Constructor call.

From: Date: Sun, 26 Jan 2014 03:11:22 +0000
Subject: Bug #66577 [Opn]: Variable assignment in Constructor call.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184010@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66577&edit=1 ID: 66577 User updated by: pete at petermcdonald dot co dot uk Reported by: pete at petermcdonald dot co dot uk Summary: Variable assignment in Constructor call. Status: Open Type: Bug Package: Class/Object related Operating System: linux PHP Version: Irrelevant Block user comment: N Private report: N New Comment: My apologies the code would be better read as: <?php //No constructor class Foo {} $a = null; $f = new Foo($a = 123); var_dump($a); // NULL as expected //PHP 5 style constructor class Foo2 { function __construct(){} } $a = null; $f = new Foo2($a = 123); var_dump($a); // int(123) what???? // PHP 4 style constructor class Foo3 { function Foo3(){} } $a = null; $f = new Foo3($a = 123); var_dump($a); // int(123) what???? Previous Comments: ------------------------------------------------------------------------ [2014-01-26 03:06:10] pete at petermcdonald dot co dot uk Description: ------------ In all honesty this is not my discovery however located at https://www.facebook.com/permalink.php?story_fbid=215062192030452&id=147594855443853 however I have been unable to locate a bug report for this. It appears that the scripts that do contain a constructor (both old style php 4 and new style php 5) over ride the value of a variable in the main scope of the script. This appears to be caused by the user specifying a default value when calling the constructor instead of specifying in the constructor itself. Would it not be beneficial to report an error rather than cause the value being assigned to the variable in the main scope of the script. Test script: --------------- <?php class Foo {} $a = null; $f = new Foo($a = 123); var_dump($a); // NULL as expected class Foo { function __construct(){} } $a = null; $f = new Foo($a = 123); var_dump($a); // int(123) what???? class Foo2 { function Foo2(){} } $a = null; $f = new Foo($a = 123); var_dump($a); // int(123) what???? Expected result: ---------------- NULL NULL NULL Actual result: -------------- NULL int(123) int(123) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66577&edit=1

« previous php.bugs (#184010) next »