PHP: 无法访问导入的 ini 值
PHP: Can't access imported ini values
我确定这是一个简单的问题,但我是 PHP 的新手,无法弄明白。我正在尝试解析一个 .ini 文件,它工作正常,但我无法访问这些值。
我的网站结构如下:
project/
index.php
inc/
init.php
classes/
frontend.php
msg/
messages.ini
消息文件在 init.php
中用 $messages = parse_ini_file("msg/messages.ini");
解析,然后像这样包含在 index.php
中:
<?php
require_once('inc/init.php');
//printing the array here works!
$html = new frontend();
如果我从 index.php
打印消息数组,一切正常。
然而,当 index.php
构建 new frontend();
时,$messages
数组在那里不可用。 frontend
本身在 init.php
中加载并且在没有 ini 文件的情况下也能正常工作。
因此我假设存在导入或变量范围问题,但我无法弄清楚。有人能指出我正确的方向吗?
尝试在 frontend()
内将 $messages
声明为全局。
而不是:
$html = new frontend();
...做类似的事情:
$html = new frontend($messages);
PHP variable scope 无论如何都相当简单:变量对于 function/method.
来说要么是全局的,要么是局部的
我确定这是一个简单的问题,但我是 PHP 的新手,无法弄明白。我正在尝试解析一个 .ini 文件,它工作正常,但我无法访问这些值。
我的网站结构如下:
project/
index.php
inc/
init.php
classes/
frontend.php
msg/
messages.ini
消息文件在 init.php
中用 $messages = parse_ini_file("msg/messages.ini");
解析,然后像这样包含在 index.php
中:
<?php
require_once('inc/init.php');
//printing the array here works!
$html = new frontend();
如果我从 index.php
打印消息数组,一切正常。
然而,当 index.php
构建 new frontend();
时,$messages
数组在那里不可用。 frontend
本身在 init.php
中加载并且在没有 ini 文件的情况下也能正常工作。
因此我假设存在导入或变量范围问题,但我无法弄清楚。有人能指出我正确的方向吗?
尝试在 frontend()
内将 $messages
声明为全局。
而不是:
$html = new frontend();
...做类似的事情:
$html = new frontend($messages);
PHP variable scope 无论如何都相当简单:变量对于 function/method.
来说要么是全局的,要么是局部的