dongshie8450 2016-02-12 17:13
浏览 99
已采纳

变量范围:require_once不加载我的变量,但需要加载

I know this is something silly, but I can't figure it out. I found a few similar questions all in the context of an MVC framework. That's my case as well, as I am using CodeIgniter.

I have a file questions.php (that's included in a view):

require_once '../site_init.php';

var_dump($siteVars);
// shows null and a Notice: Undefined variable: siteVars
// but the ABSPATH constant is showing as defined!
var_dump(ABSPATH);
// shows string 'c:\wamp\www\sitename'

require '../site_init.php';
var_dump($siteVars);
// correctly dumps the content of siteVars array

and a file site_init.php that should be included everywhere as it holds my site-wide configuration values:

if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

/** Site-wide sitevars */
$siteVars = array();

// set to true in develop environment
$siteVars['debug'] == false;

I know that The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again however, when I am using require_once, I get a notice saying Undefined variable: siteVars, while using require, all works as expected. However, as you can see in the code above, the constant shows as defined, although they are both defined in the same file. PHP manual: Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope.

print_r(get_included_files()); shows site_init.php was included before the require_once, so I shouldn't have to require(_once) it again.

It must have something to do with variable scope. If I use global $siteVars, it works, without the need to require the file again, but can someone explain exactly why this happens? I am new to CodeIgniter. I can see there is only one entry point (the main index.php file) and that's the base file ($_SERVER['PHP_SELF']).

Ideally, I would also like to know how I can fix this without using global or require.

UPDATE: File structure seems to be the following (this is a project I am only working on, I am not the original developer):

  • controller welcome.php loads (include_once) a file X outside the CodeIgniter app folder structure (the CI app is the admin part of a larger site).

  • file X include_once site_init.php file

  • controller welcome.php loads the view $this->load->view('template', $data);

  • this is pretty much it. Hope this holds the key to a solution.

  • 写回答

2条回答 默认 最新

  • douting1871 2016-02-12 20:05
    关注

    In CodeIgniter the only variables accessible in a view are passed to it from the controller. There should never be a reason to include anything in this way in COdeIgniter

    Controller:

    $d['title'] = 'title';    
    $this->load->view('main',$d);
    

    View:

    <?php print $title;?>
    

    see Config Class http://www.codeigniter.com/user_guide/libraries/config.html for custom config values which could then be accessed in the controller and passed on to the view

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器