我正在使用 Slim 3 构建一个休息 API,我有这个结构
# models/user.php
<?php
class User {
public $id;
public $username;
public $password;
public $number;
public $avatar;
function __construct($id, $username, $password, $number, $avatar = null, $active = false) {
$this -> id = $id;
$this -> username = $username;
$this -> password = $password;
$this -> number = $number;
$this -> avatar = $avatar;
$this -> active = $active;
}
static function getByUsername($username) {
// i want to access the container right here
}
}
?>
我不能将用户模型存储在依赖容器中,因为我不能在 PHP 中有多个构造函数,而且我不能从类实例访问静态方法?那么如何从无法存储在依赖容器中的服务访问容器?