php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11092 (Yet Another) References Weirdness
Submitted: 2001-05-24 13:04 UTC Modified: 2001-05-24 15:23 UTC
From: sbergmann@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.5 OS: should be OS independent
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: sbergmann@php.net
New email:
PHP Version: OS:

 

 [2001-05-24 13:04 UTC] sbergmann@php.net
There seems to be a problem with objects returned from class methods by reference:

<?php
class a {
  var $data;

  function add_data($data) {
    $this->data[] = $data;
  }
}

class b {
  var $a;

  function &add_a() {
    $this->a = new a;
    return $this->a;
  }

  function dump() {
    print_r($this->a->data);
  }
}

$b = new b;
$a = $b->add_a();
$a->add_data('test');    // works not
$b->a->add_data('test'); // works
$b->dump();
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-24 13:18 UTC] sbergmann@php.net
Just noticed: It works as expected when I replace

  $a = $b->add_a();

with

  $a =& $b->add_a();

But this is not the syntax I was thinking of for my class. The user, which probably doesn't know of the class's internals, now has always use the =& assignment operator. This is definetly error-prone.

 [2001-05-24 15:23 UTC] rasmus@php.net
This is consistent with the non-OOP syntax.  I wouldn't consider this a bug.  At call-time you have the choice of whether you want to use the reference or not.  I do agree with you that the syntax is horribly ugly.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 02:01:28 2025 UTC