Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having some trouble with references in PHP. Let's say you do the following:

<?php

class foo
{
    public $str;
}

class bar
{
    public $txt;
}

class x
{
    private $objs = array();

    public function add(&$obj)
    {
        $this->objs[] = $obj;
    }

    public function update()
    {
        foreach($this->objs as $obj)
        {
            $obj = new bar();
            $obj->txt = "bar";
        }
     }
}

//Make x
$x = new x();

//Make foo
$foo = new foo();
$foo->str = "foo";

//Add
$x->add($foo);

//update
$x->update();

var_dump($foo);

The var_dump at the end gives:

class foo#2 (1) {
  public $str =>
  string(3) "foo"
}

Evidently, I'm not storing my reference properly in the array. Is there a way to do this so that $foo is of type bar after $x->update()?

share|improve this question
    
Interesting. I don't think you can do this. Objects are already references. Basically you need a reference of reference. –  Ivarpoiss Mar 16 at 2:43
    
@Ivarpoiss: "Objects are already references." That doesn't make sense. Objects are objects. Object references are pointers to objects. "Objects" are not values in PHP5. Object pointers (references) are. Object pointers are values like any other values, and variables containing them can be passed or assigned by reference as desired. –  newacct Mar 18 at 6:27
    
Mindnumbing stuff. –  Ivarpoiss Mar 18 at 7:09

1 Answer 1

up vote 1 down vote accepted

Try this. I don't know why or how this works. Just added some '&'s all over the place.

public function add(&$obj)
{
    $this->objs[] = &$obj;
}

public function update()
{
    foreach($this->objs as &$obj)
    {
        $obj = new bar();
        $obj->txt = "bar";
    }
}

UPDATE

An object is actually only logically(to us) a reference, but technically different from references in php.

PHP manual explains this. Object variables are different from references. Reference is an alias to a variable. It points to the same data held by that variable. Object is a datatype that holds object identifiers. So you can also have references to objects.

$a = new foo();
$b = $a;

$a and $b here are 2 different variables both having the same value (object id), not aliases or reference types. This works just like $a = 1;$b = $a; .

Arrays can store references. By doing var_dump($x->objs); you can see the object references in array.

You don't often see references of objects in var_dump outputs because references get constantly dereferenced by assignments. You also can't do a call-time pass-by-reference to functions like var_dump and array_push.

Your problem was caused by the dereferencing and missing the additional reference operators.

share|improve this answer
    
That sure does it, I also managed to get something similar going with an in-between function, but this is pretty clean. Can anyone pipe in on some theory here, since PHP claims to not support references to references? –  SuperTron Mar 16 at 3:01
    
I did some research. –  Ivarpoiss Mar 16 at 3:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.