PHP和一些全局变量

发布于 2024-08-03 06:45:40 字数 761 浏览 4 评论 0原文

require_once'modules/logger.php';                                                         
$Logger = new Logger();

require_once 'templates/list.php';
$Templates = new templatesList();

require_once 'widgets/list.php';
$Widgets = new widgetsList();

我在 templates/list.phpwidgets/list.php 中使用 $Logger$Templates 我在 /widgets/list.php 中使用。

上面的代码抛出这个错误:

注意:未定义的变量:Logger in .../templates/list.php 第 99 行 致命错误:调用成员函数 中非对象的 toLog() .../templates/list.php 第 99 行

UPD 这是第 99 行:

$Logger->toLog( $contentData );
require_once'modules/logger.php';                                                         
$Logger = new Logger();

require_once 'templates/list.php';
$Templates = new templatesList();

require_once 'widgets/list.php';
$Widgets = new widgetsList();

I use $Logger in templates/list.php and in widgets/list.php.
$Templates I use in /widgets/list.php.

The code above throws this error:

Notice: Undefined variable: Logger in
.../templates/list.php on line 99
Fatal error: Call to a member function
toLog() on a non-object in
.../templates/list.php on line 99

UPD
Here is line 99:

$Logger->toLog( $contentData );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-08-10 06:45:40

如果您想在对象方法中使用 $Logger,则需要在该方法中将其标记为全局。如果不这样做,您最终将创建一个新的本地 $Logger 变量。我怀疑这就是问题所在。例如:

class templatesList {
    public function __construct() {
        global $Logger;
        //now we can use $logger.
    }
}

但是,最好将 $Logger 传递到需要使用它的每个对象的构造函数中。全局变量通常不被认为是好的做法。

class templatesList {
    protected $Logger;
    public function __construct(Logger $Logger) {
        //now we can use $logger.

        //store reference we can use later
        $this->Logger = $Logger;
    }

    public function doSomething() {
        $this->Logger->log('something');
    }
}

new templatesList($Logger);

If you want to use the $Logger from within an object method, you will need to mark it as global within that method. If you don't you will end up creating a new local $Logger variable. I suspect that is what the problem is. For example:

class templatesList {
    public function __construct() {
        global $Logger;
        //now we can use $logger.
    }
}

However, it would probably be better to pass $Logger into the constructor of every object which needs to use it. Global variables are not generally considered good practise.

class templatesList {
    protected $Logger;
    public function __construct(Logger $Logger) {
        //now we can use $logger.

        //store reference we can use later
        $this->Logger = $Logger;
    }

    public function doSomething() {
        $this->Logger->log('something');
    }
}

new templatesList($Logger);
对你而言 2024-08-10 06:45:40

您确定没有在 /templates/list.php 开头的某个位置取消设置 $Logger 吗?

在初始化之后和使用之前尝试 var_dumping() $Logger。

Are you sure you're not unsetting $Logger somewhere in the beginning of /templates/list.php?

Try var_dumping() $Logger after its initialization and before using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文