RE: [PHP] Scope problem in while loop
| From: | Demitrious S. Kelly | Date: | Tue, 12 Mar 2002 03:38:30 +0000 |
| Subject: | RE: [PHP] Scope problem in while loop | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-88142@lists.php.net to get a copy of this message | ||
I may be wrong, but that's exactly what I ended up having to do... but
don't quote me - I'm just learning OOP
http://www.apokalyptik.com/forum/viewtopic.php?topic=140&forum=6&0
-----Original Message-----
From: Randall Perry [mailto:rgp@systame.com]
Sent: Monday, March 11, 2002 6:52 PM
To: php-general@lists.php.net
Subject: [PHP] Scope problem in while loop
According to the PHP 4 docs all variables are global unless within a
function. I've got the following test code which should output 2, but
outputs 1. The while loop creates it's own class object (which seems
strange
since it isn't explicitly instantiated by my code; I would think it
would
cause errors).
The reason I created an object for my variable (actually, I started
testing
with regular strings) is that I've had similar scoping problems in perl,
where variables got out of scope within loops (or even if statements).
In
perl, declaring an object outside these structures will protect it's
scope.
Not so in PHP I see.
Can anyone explain this behavior? Do I have to create functions that
return
values every time I need a loop that modifies a variable?
<?php
class Ccust_data {
function Cform_data() {
$this->test = "";
}
}
$o = new Ccust_data();
$o->test = 1;
while ($y = 0) {
global $o->test;
$o->test = 2;
$y = 1;
}
echo "\$o->test = $o->test\n";
?>
--
Randy Perry
sysTame
Mac Consulting/Sales
phn 561.589.6449
mobile email help@systame.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php