在 PHP 中,报错:”Class ‘weibo\\rests\\oauth2\\DateTime’ not found” 的分析与解决

1、在 Yii 2 中,报错:”Class ‘weibo\\rests\\oauth2\\DateTime’ not found”

$urlSigner->signParams($route, false, (new DateTime())->add(new DateInterval('PT1H')));
{
    "name": "Exception",
    "message": "Class 'weibo\\rests\\oauth2\\DateTime' not found",
    "code": 0,
    "type": "Error",
    "file": "E:\\wwwroot\\channel-pub-api\\weibo\\rests\\oauth2\\AuthorizeAction.php",
    "line": 129,
    "stack-trace": [
        "#0 [internal function]: weibo\\rests\\oauth2\\AuthorizeAction->run()",
        "#1 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Action.php(94): call_user_func_array(Array, Array)",
        "#2 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\Action->runWithParams(Array)",
        "#3 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('authorize', Array)",
        "#4 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/oauth2/autho...', Array)",
        "#5 E:\\wwwroot\\channel-pub-api\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
        "#6 E:\\wwwroot\\channel-pub-api\\weibo\\web\\index.php(17): yii\\base\\Application->run()",
        "#7 {main}"
    ]
}

2、由于使用了命名空间,要解决在命名空间中查找类定义的问题,需要在类名之前加上前导 \

$urlSigner->signParams($route, false, (new \DateTime())->add(new \DateInterval('PT1H')));
永夜