f



When "static" isn't "static"

[PHP]
function blah($item) {
  	if (!isset($baseDir)) {
     	 static $baseDir = '';
     	 $baseDir = $item;
	 print_r("baseDir = $baseDir\n");
    	}

       $dirID = opendir($item);
       while (($fyl = readdir($dirID)) !== false) {
         if (is_dir("$baseDir/$fyl")) blah($item);
         // DO OTHER STUFF IF IT IS A FILE
       }
}
[/PHP]

I am using PHP 4.3.2 and the following occurs within blah() :

[Quote]
baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..
[/Quote]

I was under the impression that a static variable retains its value
throughout a function (or method) recursive memory stack.  However, it
changes everytime because it apparently loses its set value.

Needless to mention I'm confused.  Was I wrong about static; if so,
what else do I do to retain this value? I cannot use $_SESSION as this
is command-line PHP instead of API.

Thanx
Phil

0
12/5/2005 7:02:31 PM
comp.lang.php 32646 articles. 0 followers. Post Follow

11 Replies
673 Views

Similar Articles

[PageSpeed] 33

On 5 Dec 2005 11:02:31 -0800, "comp.lang.php"
<phillip.s.powell@gmail.com> wrote:

>I was under the impression that a static variable retains its value
>throughout a function (or method) recursive memory stack.  However, it
>changes everytime because it apparently loses its set value.

Try it this way:

[PHP]
function blah($item) {
	static $baseDir = '';
  	if (!$baseDir) {
     	 $baseDir = $item;
	 print_r("baseDir = $baseDir\n");
    	}

       $dirID = opendir($item);
       while (($fyl = readdir($dirID)) !== false) {
         if (is_dir("$baseDir/$fyl")) blah($item);
         // DO OTHER STUFF IF IT IS A FILE
       }
}
[/PHP]

0
not32 (1022)
12/5/2005 7:17:25 PM
Wayne wrote:
> On 5 Dec 2005 11:02:31 -0800, "comp.lang.php"
> <phillip.s.powell@gmail.com> wrote:
>
> >I was under the impression that a static variable retains its value
> >throughout a function (or method) recursive memory stack.  However, it
> >changes everytime because it apparently loses its set value.
>
> Try it this way:
>
> [PHP]
> function blah($item) {
> 	static $baseDir = '';
>   	if (!$baseDir) {
>      	 $baseDir = $item;
> 	 print_r("baseDir = $baseDir\n");
>     	}

Sorry that had a worse effect, I'm afraid:

[Quote]
baseDir = "/home/me/stuff"
baseDir = "/."
baseDir = "/.."
[/Quote]

That tried to attempt to alter the root directory instead of the given
base directory.

Phil

>
>        $dirID = opendir($item);
>        while (($fyl = readdir($dirID)) !== false) {
>          if (is_dir("$baseDir/$fyl")) blah($item);
>          // DO OTHER STUFF IF IT IS A FILE
>        }
> }
> [/PHP]

0
12/5/2005 7:21:03 PM
comp.lang.php said the following on 05/12/2005 19:02:
> [PHP]
> function blah($item) {
>   	if (!isset($baseDir)) {
>      	 static $baseDir = '';
>      	 $baseDir = $item;
> 	 print_r("baseDir = $baseDir\n");
>     	}
> 
>        $dirID = opendir($item);
>        while (($fyl = readdir($dirID)) !== false) {
>          if (is_dir("$baseDir/$fyl")) blah($item);
>          // DO OTHER STUFF IF IT IS A FILE
>        }
> }
> [/PHP]
> 
> I am using PHP 4.3.2 and the following occurs within blah() :
> 
> [Quote]
> baseDir = /home/me/stuff
> baseDir = /home/me/stuff/.
> baseDir = /home/me/stuff/./..
> [/Quote]
> 
> I was under the impression that a static variable retains its value
> throughout a function (or method) recursive memory stack.  However, it
> changes everytime because it apparently loses its set value.

You are using $baseDir before you have declared it static, so PHP treats 
it as non-static.


-- 
Oli
0
catch (918)
12/5/2005 7:31:24 PM
I can see what you are trying to do however it's not going to work
the way you have it. Sure its static var but its going to change every
time it you recursivly call blah (due to this line $baseDir = $item; &
this line  if (is_dir("$baseDir/$fyl")) blah($item); );

(e.g. everytime it finds a dir its going to change the value of
baseDir).

0
12/5/2005 7:32:30 PM
Why do you need this? What do you want to do? Do you want to
recursively walk trough directories? In that case, here is an example:

function traverse($dir) {
	$dh = opendir($dir);
	while ($file = readdir($dh)) {
		if ($file{0} == '.') continue;
		$path = $dir.'/'.$file;
		if (is_file($path)) {
			echo $file."\n";
		} else {
			traverse($path);
		}
	}
	closedir($dh);
}

0
sjoerder (227)
12/5/2005 7:37:23 PM
Wow, ok, that was a surprise!  I didn't think isset() had the ability
to declare a variable on the fly like that.  I was under the impression
that it only checked for instantiation, which would be implied that if
a variable is not yet declared, it certainly could never be
instantiated as you cannot instantiate a nonexistent entity.

That solved the problem but potentially opened up a big PHP logic flaw
in the process.

Thanx!
Phil

Oli Filth wrote:
> comp.lang.php said the following on 05/12/2005 19:02:
> > [PHP]
> > function blah($item) {
> >   	if (!isset($baseDir)) {
> >      	 static $baseDir = '';
> >      	 $baseDir = $item;
> > 	 print_r("baseDir = $baseDir\n");
> >     	}
> >
> >        $dirID = opendir($item);
> >        while (($fyl = readdir($dirID)) !== false) {
> >          if (is_dir("$baseDir/$fyl")) blah($item);
> >          // DO OTHER STUFF IF IT IS A FILE
> >        }
> > }
> > [/PHP]
> >
> > I am using PHP 4.3.2 and the following occurs within blah() :
> >
> > [Quote]
> > baseDir = /home/me/stuff
> > baseDir = /home/me/stuff/.
> > baseDir = /home/me/stuff/./..
> > [/Quote]
> >
> > I was under the impression that a static variable retains its value
> > throughout a function (or method) recursive memory stack.  However, it
> > changes everytime because it apparently loses its set value.
>
> You are using $baseDir before you have declared it static, so PHP treats
> it as non-static.
> 
> 
> -- 
> Oli

0
12/5/2005 7:38:23 PM
No I'm sorry I think you misunderstood my problem.  I wound up having
it solved earlier; the problem had to do with the way I was using the
static option, which I still believe illuminates a potential PHP logic
flaw when it comes to using "static".

Phil

Sjoerd wrote:
> Why do you need this? What do you want to do? Do you want to
> recursively walk trough directories? In that case, here is an example:
>
> function traverse($dir) {
> 	$dh = opendir($dir);
> 	while ($file = readdir($dh)) {
> 		if ($file{0} == '.') continue;
> 		$path = $dir.'/'.$file;
> 		if (is_file($path)) {
> 			echo $file."\n";
> 		} else {
> 			traverse($path);
> 		}
> 	}
> 	closedir($dh);
> }

0
12/5/2005 7:40:05 PM
comp.lang.php said the following on 05/12/2005 19:38:
> Wow, ok, that was a surprise!  I didn't think isset() had the ability
> to declare a variable on the fly like that.

It doesn't.

>  I was under the impression
> that it only checked for instantiation

It does.

> which would be implied that if
> a variable is not yet declared, it certainly could never be
> instantiated as you cannot instantiate a nonexistent entity.

??

> 
> That solved the problem but potentially opened up a big PHP logic flaw
> in the process.
> 
> 
> Oli Filth wrote:
> 
>>comp.lang.php said the following on 05/12/2005 19:02:
>>
>>>[PHP]
>>>function blah($item) {
>>>  	if (!isset($baseDir)) {
>>>     	 static $baseDir = '';
>>>     	 $baseDir = $item;
>>>	 print_r("baseDir = $baseDir\n");
>>>    	}
>>>
>>>       $dirID = opendir($item);
>>>       while (($fyl = readdir($dirID)) !== false) {
>>>         if (is_dir("$baseDir/$fyl")) blah($item);
>>>         // DO OTHER STUFF IF IT IS A FILE
>>>       }
>>>}
>>>[/PHP]
>>>
>>>I am using PHP 4.3.2 and the following occurs within blah() :
>>>
>>>[Quote]
>>>baseDir = /home/me/stuff
>>>baseDir = /home/me/stuff/.
>>>baseDir = /home/me/stuff/./..
>>>[/Quote]
>>>
>>>I was under the impression that a static variable retains its value
>>>throughout a function (or method) recursive memory stack.  However, it
>>>changes everytime because it apparently loses its set value.
>>
>>You are using $baseDir before you have declared it static, so PHP treats
>>it as non-static.
>>
>>
>>-- 
>>Oli
> 
> 


-- 
Oli
0
catch (918)
12/5/2005 7:56:07 PM
I'm sorry I do not understand what you are trying to say.

Phil

Oli Filth wrote:
> comp.lang.php said the following on 05/12/2005 19:38:
> > Wow, ok, that was a surprise!  I didn't think isset() had the ability
> > to declare a variable on the fly like that.
>
> It doesn't.
>
> >  I was under the impression
> > that it only checked for instantiation
>
> It does.
>
> > which would be implied that if
> > a variable is not yet declared, it certainly could never be
> > instantiated as you cannot instantiate a nonexistent entity.
>
> ??
>
> >
> > That solved the problem but potentially opened up a big PHP logic flaw
> > in the process.
> >
> >
> > Oli Filth wrote:
> >
> >>comp.lang.php said the following on 05/12/2005 19:02:
> >>
> >>>[PHP]
> >>>function blah($item) {
> >>>  	if (!isset($baseDir)) {
> >>>     	 static $baseDir = '';
> >>>     	 $baseDir = $item;
> >>>	 print_r("baseDir = $baseDir\n");
> >>>    	}
> >>>
> >>>       $dirID = opendir($item);
> >>>       while (($fyl = readdir($dirID)) !== false) {
> >>>         if (is_dir("$baseDir/$fyl")) blah($item);
> >>>         // DO OTHER STUFF IF IT IS A FILE
> >>>       }
> >>>}
> >>>[/PHP]
> >>>
> >>>I am using PHP 4.3.2 and the following occurs within blah() :
> >>>
> >>>[Quote]
> >>>baseDir = /home/me/stuff
> >>>baseDir = /home/me/stuff/.
> >>>baseDir = /home/me/stuff/./..
> >>>[/Quote]
> >>>
> >>>I was under the impression that a static variable retains its value
> >>>throughout a function (or method) recursive memory stack.  However, it
> >>>changes everytime because it apparently loses its set value.
> >>
> >>You are using $baseDir before you have declared it static, so PHP treats
> >>it as non-static.
> >>
> >>
> >>-- 
> >>Oli
> > 
> > 
> 
> 
> -- 
> Oli

0
12/5/2005 8:19:10 PM
comp.lang.php said the following on 05/12/2005 20:19:
> I'm sorry I do not understand what you are trying to say.
> 
> 
> Oli Filth wrote:
> 
>>comp.lang.php said the following on 05/12/2005 19:38:
>>
>>>Wow, ok, that was a surprise!  I didn't think isset() had the ability
>>>to declare a variable on the fly like that.
>>
>>It doesn't.

As in: isset() doesn't have the ability to declare a variable on the fly.

>>
>>
>>> I was under the impression
>>>that it only checked for instantiation
>>
>>It does.

As in: isset() does only check for instantiation.


>>
>>>which would be implied that if
>>>a variable is not yet declared, it certainly could never be
>>>instantiated as you cannot instantiate a nonexistent entity.
>>
>>??

As in: this sentence makes no sense!!



-- 
Oli
0
catch (918)
12/5/2005 9:48:02 PM
What Oli meant was that the line "static $baseDir" doesn't take effect
until it's reached, hence !isset($baseDir) will always be true in your
code. Better think of it as an operator than a declaration.

0
chernyshevsky (2297)
12/6/2005 12:43:48 AM
Reply: