无法在 PHP 错误中重新声明 class
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行。看起来下面有额外的代码。考虑删除多余的行
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行。看起来下面有额外的代码。考虑删除多余的行