By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,041 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,041 IT Pros & Developers. It's quick & easy.

When "static" isn't "static"

P: n/a
[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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.