简体   繁体   English

PHP致命错误:找不到类'X'

[英]PHP Fatal error: Class 'X' not found

I am facing a very strange error although it seems that i have done all the requires 尽管似乎已满足所有要求,但我遇到了一个非常奇怪的错误

Error: PHP Fatal error: Class 'Login' not found 错误: PHP Fatal error: Class 'Login' not found

Code: 码:

<?php

//Includes...
require_once(__DIR__ . "/base/request.abstract.php");

//Entry point...
try {
    //What should i do ? the class is in the same file so what to do so php recognize it ?
    echo (new Login($_REQUEST['request']))->processRequest();
} catch (Exception $e) {
    echo json_encode(array('error' => $e->getMessage()));
}

//Implementation...
class Login extends RequestAbstract
{
    public function processRequest()
    {

    }
}

You're trying to use your Login class before it's been declared. 您试图在声明您的Login类之前使用它。 Move your class above the try/catch block, and your code will work. 将您的类移到try / catch块上方,您的代码将起作用。 As the manual says , "Classes should be defined before instantiation (and in some cases this is a requirement)." 正如手册所说 :“类应该在实例化之前定义(在某些情况下,这是必需的)。”

Defining a class before it is instantiated is a requirement when a Class is implementing an interface. 当类实现接口时,需要在实例化之前定义类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM