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()
?