|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2014-01-26 03:06 UTC] 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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 15:00:01 2025 UTC | 
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????