【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
同目录下,有conf.php,T.php
conf.php
<?php $conf=array( 'name'=>'hk', 'age'=>'22' );
<?php function getConf($key){ static $conf=''; if(empty($conf)){ require_once 'conf.php'; } return $conf[$key]; } function getConf1($key){ require_once 'conf.php'; return $conf[$key]; } echo getConf1('name'); echo getConf('name');
在getConf1函数作用域内(域1)引入了 conf.php
但在调用getConf时 在getConf作用域(域2) 里 它判断 conf.php已经引入过
所就不在引入了
导致执行getConf是,$conf一直为空
难道所有函数的作用域是连在一起的,还是?
static静态变量,只读,所以在getConf()函数里面include进去的是不会覆盖getConf()的。
2楼的兄弟,require_once 不是只能被引入一次
引用来自“酒逍遥”的评论
因为 你用了 require_once 文件就只能被引入一次..
改用 require 就可以了.
为什么函数之间的会受影响
因为 你用了 require_once 文件就只能被引入一次..
改用 require 就可以了.