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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rophp at live dot it
New email:
PHP Version: OS:

 

 [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

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 Sep 07 23:01:27 2024 UTC