Bug #66577 [Nab]: Variable assignment in Constructor call.
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: Not a bug
Type: Bug
Package: Class/Object related
Operating System: linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I am sorry Krakjoe but I cannot see why the code fragment you have provided is relevant to this
report.
In the code I mention a variable in the main scope only changes value when a constructor is present.
Your code is simply comparing values.
The behavior specified in the bug report causes inconsistent behavior and has the potential to
introduce bugs.
Why should it matter if a constructor is present for a variable to change value in this manner?
Previous Comments:
------------------------------------------------------------------------
[2014-02-02 10:09:42] krakjoe@php.net
This isn't a bug, it's extremely beneficial behaviour you are just noticing:
https://gist.github.com/krakjoe/c6bcbb4e6d019c9297db
Thanks for taking the time to make PHP better :)
------------------------------------------------------------------------
[2014-01-27 00:05:40] pete at petermcdonald dot co dot uk
Ok fair enough, maybe more bad practice to assign a value to a variable in object initialization.
But yes there should be consistency regardless upon the existence of a constructor.
------------------------------------------------------------------------
[2014-01-26 03:36:13] requinix@php.net
> specifying a default value when calling the constructor
There is no such thing. What you are doing is exactly what the summary says: variable assignment ($a
= 123) and then constructing the objects (with the value 123). It is - should be - identical to
writing
$a = null;
$a = 123;
$f = new Foo(123);
The bug I see here is that the first example does not do the assignment.
http://3v4l.org/IFS07
------------------------------------------------------------------------
[2014-01-26 03:11:21] pete at petermcdonald dot co dot uk
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????
------------------------------------------------------------------------
[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
Thread (6 messages)