views:

25

answers:

1
+2  A: 

What you probably want is this:

class MyStack{
    /* ... */

    /* Store a non-reference */
    public function push($elem) {
        $this->_storage[] = $elem;
    }

    /* return a reference */
    public function &top(){
        return $this->_storage[count($this->_storage)-1];
    }

    /* ...*/
}

/* You must also ask for a reference when calling */
/* ... */
$t = &$stack->top();
$t[] = 2;
Artefacto
@irc Yes, you do. See http://codepad.viper-7.com/kVQe4g
Artefacto
Fair point. I just tried. You are correct sir... (+1)
ircmaxell
@user Because `=` is the assignment operator, which doesn't do assignment by reference. If you do `$a = 1; $b =
Artefacto
Ya Its getting Segfault.
@user Can you post a small script that reproduces the problem and post it somewhere (e.g. pastebin)?
Artefacto