pointer behavior in PHP?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi, I'm fairly new to PHP and I have a question regarding pointers.
Below is an example binary tree implementation I found in the
internet. It works great but I'm not sure how it works. Here's what
I'm confused about, in the insert function the variable $pointer is
set to $this->tree. Now, my current understanding is that after this
the variable $pointer will contain the data from $this-> tree.
However, as the function continues $pointer is set to pointer->left
(or right) until the correct index is found. at this point the value
is added to the tree with $pointer->left=new leaf.

In this case the value of pointer seems to be referencing a position
in the $tree variable instead of merely containing the data of $tree
as I would expect. Furthermore, when $pointer->left is set to a new
leaf, it is actually added to $tree and not just to $pointer. So my
question is, why is this variable behaving like a pointer in this case
instead of just containing the value of tree?


class leaf
    public $data;
    public $left;
    public $right;

    public function __construct($d){
        $this->data = $d;
class binary_tree{
    public $tree=Null;
    public function insert($val) {
            if (!(isset($this->tree))) {
                $this->tree = new leaf($val);
        } else {
                $pointer = $this->tree;
                for(;;) {
                    if ($val <= $pointer->data) {
                        if ($pointer->left) {$pointer = $pointer->left;}
                else {$pointer->left = new leaf($val); break;}
                    } else {
                        if ($pointer->right) {$pointer = $pointer-
Quoted text here. Click to load it
                else {$pointer->right = new leaf($val); break;}

$root=new binary_tree;
for($i=0; $i<20; $i++){
    $randnum = rand(0,1000);
print "<pre>";

Re: pointer behavior in PHP?


Quoted text here. Click to load it

Nope. The tree is built with objects and objects in PHP 5 are always
addressed by a kind of reference (internally it's just a numeric handle
that identifies the object). So with

$pointer = $this->tree;

both variables simply reference the same root node of the tree. There's
still only one tree of objects.

Quoted text here. Click to load it


Correct. $pointer always references a node of the tree. All of them are
of the class 'leaf' and have the defined properties. So regardless which
node is referenced by the $pointer variable, there's always a $data,
$left and $right property.

Quoted text here. Click to load it

Try to understand how objects are handled and addressed in PHP 5:

$foo = new Test();
$bar = $foo;

The first line creates a new object and assigns its internal number (the
object handle, for example 42) to $foo, hence $foo kinda references the
object with the internal number 42.

The second line simply copies the number stored in $foo to $bar, so now
both variables have the same value and therefore reference the same
object. The object itself is never copied unless you explicitly clone

$foo = new Test();
$bar = clone $foo;

Now both variables reference _different_ objects.


Re: pointer behavior in PHP?

Quoted text here. Click to load it

Thanks a bunch, that clears things up!


Site Timeline