php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60428 $this as byref
Submitted: 2011-12-01 15:01 UTC Modified: 2012-02-10 23:42 UTC
From: bjorn at hcbmedia dot nl Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.3.8 OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-12-01 15:01 UTC] bjorn at hcbmedia dot nl
Description:
------------
take a look at the following script

<?php
class B {
    public $b = 50;

    public function whoami () {
        var_dump (__CLASS__);
    }
}

class A {
    public $a = 6;

    function thisref () {
        var_dump ($this); // A
        $this->ref ($this); // exchange $this by a B object
        var_dump ($this); // B
        var_dump ($this->a); // 6
        var_dump ($this->b); // Notice: Undefined property: A::$b
        var_dump (isset ($this->a)); // true
        var_dump (property_exists ($this, "a")); // false
        var_dump (__CLASS__); // A
        $this->whoami (); // A
        var_dump ($this instanceof self ? "A" : "B"); // B
    }

    function ref (A &$ref) {
        $ref = new B();
    }

    public function whoami () {
        var_dump (__CLASS__);
    }
}

$a = new A();
$a->thisref ();
?>

it supplies $this variable as a property to a byref function, then changing it to a diffrent type.

if gives some strange results

Expected result:
----------------
it should not be possible to supply $this as a byref param

Actual result:
--------------
$this seems to be changed, however any call upon $this seems not, so properties and methods remain unchanged.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-10 23:42 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2012-02-10 23:42 UTC] johannes@php.net
This is caused by different optimisations for access to $this we have. The only way we could detect this is by preventing to pass $this by reference, which isn't a good restriction in the language. Afterwards, in your ref method we have no way to figure out that $this was passed. Therefore we all have to live with this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 22:01:26 2024 UTC