繁体   English   中英

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

[英]Cannot redeclare class in PHP error

PHP新手。 使用SLIM框架和路由已通过测试,并且工作正常。 有两个文件index.php和API.php。 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是:

<?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


?>

我收到错误: PHP Fatal error: Cannot redeclare class DbHandler in ../API.php on line 62 不知道为什么会这样。 我正在使用require_once并仍然收到相同的错误。 有人可以给我一些指示我可能做错了什么吗?

Api.php的代码少于62行。 看起来下面有多余的代码。 考虑删除多余的行

暂无
暂无

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

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