472,122 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

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() :

baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..
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

Dec 5 '05 #1
11 2030
On 5 Dec 2005 11:02:31 -0800, "comp.lang.php"
<ph**************@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]

Dec 5 '05 #2

Wayne wrote:
On 5 Dec 2005 11:02:31 -0800, "comp.lang.php"
<ph**************@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:

baseDir = "/home/me/stuff"
baseDir = "/."
baseDir = "/.."
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]


Dec 5 '05 #3
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() :

> baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..

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
Dec 5 '05 #4
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).

Dec 5 '05 #5
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);
}

Dec 5 '05 #6
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() :

> > baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..

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


Dec 5 '05 #7
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);
}


Dec 5 '05 #8
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() :

>>>baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..

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
Dec 5 '05 #9
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() :

> >>>baseDir = /home/me/stuff
baseDir = /home/me/stuff/.
baseDir = /home/me/stuff/./..

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


Dec 5 '05 #10
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
Dec 5 '05 #11
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.

Dec 6 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jahn Otto Næsgaard Andersen | last post: by
29 posts views Thread by Alexander Mahr | last post: by
3 posts views Thread by Ajax Chelsea | last post: by
12 posts views Thread by cppaddict | last post: by
9 posts views Thread by Neil Kiser | last post: by
14 posts views Thread by Jess | last post: by
2 posts views Thread by chenxinleo | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.