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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 22:01:27 2024 UTC