php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67418 PHP var reference in abstract class
Submitted: 2014-06-10 18:30 UTC Modified: 2014-06-10 18:45 UTC
From: rophp at live dot it Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
48 + 3 = ?
Subscribe to this entry?

 
 [2014-06-10 18:30 UTC] rophp at live dot it
Description:
------------
Basically i get "aaa" and "bbb" automatically updated also in the content object of the first $p


If i clone the var on return i can solve this, but i didn't understood why

Test script:
---------------
abstract class Tag {
    protected $content = [];

    private function parse ($var) {
        return ($var instanceof Tag) ? $var : (string)$var;
    }

    public function add_content() {
       $this->content[] = array_map([$this,'parse'],func_get_args());
    }
}

class p extends Tag { }

$p = new p;
$p2 = new p;

$p->add_content($p2);
$p2->add_content("aaa", "bbb");

print_r($p);

Expected result:
----------------
p Object ( [content:protected] => Array ( [0] => Array ( [0] => p Object ( [content:protected] => Array ( ) ) ) ) )

Actual result:
--------------
p Object ( [content:protected] => Array ( [0] => Array ( [0] => p Object ( [content:protected] => Array ( [0] => Array ( [0] => aaa [1] => bbb ) ) ) ) ) )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-10 18:45 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2014-06-10 18:45 UTC] aharvey@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected: you added $p2 to $p, and then added "aaa" and "bbb" to $p2, which is what print_r() shows if you read the indenting properly:

p Object
(
    [content:protected] => Array
        (
            [0] => Array
                (
                    [0] => p Object
                        (
                            [content:protected] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => aaa
                                            [1] => bbb
                                        )

                                )

                        )

                )

        )

)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 07:01:31 2024 UTC