PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > pointer behavior in PHP?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
pointer behavior in PHP?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/01/2008, 19h04   #1
davidgregan@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut pointer behavior in PHP?

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?

Thanks,
Dave

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-
>right;}

else {$pointer->right = new leaf($val); break;}
}
}
}
}

}
$root=new binary_tree;
for($i=0; $i<20; $i++){
$randnum = rand(0,1000);
$root->insert($randnum);
}
print "<pre>";
print_r($root)
  Réponse avec citation
Vieux 22/01/2008, 19h40   #2
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer behavior in PHP?

..oO(davidgregan@gmail.com)

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


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.

>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


Correct.

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


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.

>So my
>question is, why is this variable behaving like a pointer in this case
>instead of just containing the value of tree?


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

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

Now both variables reference _different_ objects.

Micha
  Réponse avec citation
Vieux 22/01/2008, 20h29   #3
davidgregan@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer behavior in PHP?

On Jan 22, 12:40pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(davidgre...@gmail.com)
>
> >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.

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

>
> Correct.
>
> >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.

>
> 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.
>
> >So my
> >question is, why is this variable behaving like a pointer in this case
> >instead of just containing the value of tree?

>
> 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
> it:
>
> $foo = new Test();
> $bar = clone $foo;
>
> Now both variables reference _different_ objects.
>
> Micha


Thanks a bunch, that clears things up!

Dave
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14206 seconds with 11 queries