【发布时间】:2019-08-24 20:28:54
【问题描述】:
我有一个名为 constants.php 的 PHP 脚本
constants.php:-
<?php
$projectRoot = "path/to/project/folder";
...
?>
然后我有另一个名为 lib.php 的文件
lib.php:-
<?php
class Utils {
function doSomething() {
...
// Here we do some processing where we need the $projectRoot variable.
$a = $projectRoot; //////HERE, I GET THE ERROR MENTIONED BELOW.
...
}
}
?>
然后我有另一个名为 index.php 的文件,其中包含上述两个文件。
index.php:-
<?php
...
require_once "constants.php";
...
require_once "lib.php";
(new Utils())->doSomething();
...
?>
现在,问题是当我运行 index.php 时,我得到以下错误:
注意:未定义变量:第 19 行 /var/www/html/test/lib.php 中的 projectRootPath
我的问题是为什么我会收到此错误以及如何解决?
显然,它与范围有关,但我已阅读 include 和 require 简单复制并将包含的代码粘贴到包含它的脚本中。所以我很困惑。
【问题讨论】: