简体   繁体   English

无法在PHP错误中重新声明类

[英]Cannot redeclare class in PHP error

new to PHP. PHP新手。 Using the SLIM framework and the routing has been tested and is working fine. 使用SLIM框架和路由已通过测试,并且工作正常。 Have two files index.php and API.php. 有两个文件index.php和API.php。 index.php is: index.php是:

<?php

require 'vendor/autoload.php';
require_once 'API.php';

//Turn on error checking
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

//Create a new SLIM app
$app = new \Slim\Slim();
$app->response->headers->set('Content-Type', 'application/json');


$app->get('/', function() use($app) { 
    $app->response->setStatus(200);
    echo "InstaAPI\n";
});

$app->run();

?>

API is: API是:

<?php

class DbHandler{

    protected static $conn;

    function __construct() {

        //Static self notation is different
        if(!isset(self::$conn)) {
            //same as self.connect
            $this->connect();
        }

    }

    function __destruct(){

        if(isset(self::$conn)){

            self::$conn->close();
        }

    }


    function connect(){

        $config = parse_ini_file('../../config2.ini');

        // Try and connect to the database
        self::$conn = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

        if(self::$conn===FALSE) {

            header("HTTP/1.1 500 Internal Server Error");
            header("Content-Type: application/json");
            $response = array("Response"=>"Failed to connect to the database");
            echo json_encode($response);
            die();

        }

        else{

            echo "Fine!!";
        }

    }//end connect


}//end class


?>

I am getting the error: PHP Fatal error: Cannot redeclare class DbHandler in ../API.php on line 62 . 我收到错误: PHP Fatal error: Cannot redeclare class DbHandler in ../API.php on line 62 Not sure why this is happening. 不知道为什么会这样。 I am using require_once and still getting the same error. 我正在使用require_once并仍然收到相同的错误。 Could someone give me some pointers to what I might be doing wrong please? 有人可以给我一些指示我可能做错了什么吗?

the code of Api.php is less than 62 lines. Api.php的代码少于62行。 looks like there is extra code below it. 看起来下面有多余的代码。 consider deleting extra lines 考虑删除多余的行

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

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