php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66577 Variable assignment in Constructor call.
Submitted: 2014-01-26 03:06 UTC Modified: 2014-02-03 00:39 UTC
From: pete at petermcdonald dot co dot uk Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pete at petermcdonald dot co dot uk
New email:
PHP Version: OS:

 

 [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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-26 03:11 UTC] 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:36 UTC] 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-27 00:05 UTC] 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-02-02 10:09 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2014-02-02 10:09 UTC] 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-02-03 00:39 UTC] pete at petermcdonald dot co dot uk
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?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 01 20:01:29 2024 UTC