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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀