【发布时间】:2018-10-16 19:56:20
【问题描述】:
我正在使用 Slim 3 骨架并尝试在我创建的名为 Utilities 的自定义类中使用 MonoLog。
Utilities.php - index.php 需要它
<?php
class Utilities {
protected $logger;
function __construct($c) {
$this->logger = $logger;
}
static function checkPerms() {
$this->logger->info("checkPerms() permissions of user id valid.");
return true;
}
}
Dependencies.php - 我添加了以下内容:
$container['utilities'] = function ($c) {
return new Utilities($c->get('logger'));
};
但我收到以下错误:
消息:不在对象上下文中使用 $this
文件: /Applications/MAMP/htdocs/project/src/utilities.php
我一定错过了什么,但我不确定是什么?
【问题讨论】:
-
问题是静态方法checkPerms。它不能访问 $this。只有非静态函数可以。