当“静态”时不是“静态的” [英] When "static" isn't "static"

查看:74
本文介绍了当“静态”时不是“静态的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[PHP]

函数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);

//如果它是一个文件,请做其他麻烦

}

}

[/ PHP]


我使用PHP 4.3.2并且以下内容发生在blah中( ):

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



我的印象是静态变量保留其值

整个函数(或方法)递归内存堆栈。然而,它每次都会改变
因为它显然会失去它的设定值。


不用说我很困惑。我对静态是错的;如果是的话,

我还能做些什么来保留这个价值?我不能使用$ _SESSION这个

是命令行PHP而不是API。


Thanx

Phil

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

推荐答案

item){

if(!isset(
item) {
if (!isset(


baseDir) ){

static
baseDir)) {
static


baseDir ='''';
baseDir = '''';


这篇关于当“静态”时不是“静态的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆