Re: array in the class
| From: | Toby Butzon | Date: | Sat, 02 Dec 2000 16:46:15 +0000 |
| Subject: | Re: array in the class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-28368@lists.php.net to get a copy of this message | ||
There's not a problem... let me trace through it for you; hopefully that'll
help.
: $a= new Sample("One");
: $b= new Sample("Two");
: $c= new Sample("Three");
:
: $a->set_parsed(array($b));
: $b->set_parsed(array($c));
:
: $a->print_me();
OK, it's obvious that the objects $a, $b, and $c are created. At this point,
they all have empty arrays in their $parsed elements.
When you run $a->set_parsed(array($b)), $b is copied to a new object (let's
call it $z), which is also an instance of the class Sample. So now
$a->parsed = Array( $z ), and $z->name = "Two", $z->parsed = Array( ).
Then we run $b->set_parsed(array($c)). Again, a new Sample object (this time
$y) is created with the same member values as $c; that is, $y->name =
"Three", and $y->parsed = Array( ).
So now when we run $a->print_me(), your recursive function is called:
function print_me() {
for
(reset($this->parsed);$tmp=current($this->parsed);next($this->parsed)){
$tmp->print_me();
}
print "Name: $this->name <br>\n ";
}
$a->print_me() first traverses its $a->parsed array. The array contains only the element ($z).
$z->print_me() runs. $z->parsed has no elements, so the for loop's condition fails
immediately and the for loop never runs. Then $z->name (which was "Two") is printed.
Execution returns back to the prior instance of print_me(), $a->print_me(). This function has now
completely traversed its array (it only had one element) and the for loop is completed. $a->name
(which was "One") is printed, and the original function called is finished; execution
returns back to the next line after the function call, and, in this case, stops...
Toby Butzon
[ criticism spurs improvement ]
----- Original Message -----
From: "arnost" <arnost@cs.cas.cz>
To: <php-general@lists.php.net>
Sent: Saturday, December 02, 2000 7:37 AM
Subject: [PHP] array in the class
: hi,
:
: I have defined container class Sample:
:
:
: class Sample {
: var $name;
: var $parsed = array();
:
: function Sample ($name)
: {
: $this->name = $name;
: }
:
: function set_parsed($parsed)
: {
: $this->parsed=$parsed;
: }
:
: function print_me()
: {
: for
(reset($this->parsed);$tmp=current($this->parsed);next($this->parsed)){
: $tmp->print_me();
: }
: print "Name: $this->name <br>\n ";
: }
: }
:
:
: Now I try sample usage:
:
:
: $a= new Sample("One");
: $b= new Sample("Two");
: $c= new Sample("Three");
:
: $a->set_parsed(array($b));
: $b->set_parsed(array($c));
:
: $a->print_me();
:
:
: which print the strange result:
:
: Name: Two
: Name: One
:
:
: but when I switch the set_parsed calls:
:
: $a= new Sample("One");
: $b= new Sample("Two");
: $c= new Sample("Three");
:
: $b->set_parsed(array($c));
: $a->set_parsed(array($b));
:
: $a->print_me();
:
: I obtain right (imho) result:
:
: Name: Three
: Name: Two
: Name: One
:
:
: Where is the problem?
:
: I use PHP Version 4.0.1pl2.
:
: thanks
:
:
: --
: Arnost Stedry
: Ustav informatiky
:
:
: --
: PHP General Mailing List (http://www.php.net/)
: To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
: For additional commands, e-mail: php-general-help@lists.php.net
: To contact the list administrators, e-mail: php-list-admin@lists.php.net
:
: