php 致命错误:无法声明类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38169097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Cannot declare class
提问by MadLax
I can not understand why php gives me an error
我不明白为什么 php 给我一个错误
"Fatal error: Cannot declare class rex\builder\RexBuilder, because the name is already in use in /var/www/site2.dev/App/rex/RexBuilder.php on line 12"
“致命错误:无法声明类 rex\builder\RexBuilder,因为该名称已在第 12 行的 /var/www/site2.dev/App/rex/RexBuilder.php 中使用”
RexBuilder static class, and it is called only 1 time. I did a search on the project, no longer classes with the same name.
RexBuilder 静态类,它只被调用 1 次。我对项目进行了搜索,不再是同名的类。
<?php
namespace rex\builder;
require_once 'Router.php';
use rex\router\Router;
error_reporting(E_ALL);
ini_set('display_errors', 1);
class RexBuilder {
public static function collector($array) {
$router = new Router();
foreach ($array as $key => $val) {
$router->get($val->getMethod(), $val->getInterfaces(), $val->getHandler());
}
$router->init();
}
}
?>
Call the class in index.php
调用 index.php 中的类
RexBuilder::collector(array(
new BuildModel('POST', '/api/v1/user/register', new \api\register\Registration()),
new BuildModel('POST', '/api/v1/user/login', new \api\login\Login())));
More This class is not used
More 这个类没有使用
回答by Daniel Krom
The error is thrown because of the use rex\router\Router;
duplicate classes.
由于use rex\router\Router;
重复的类而引发错误。
When you are writing use namespace..
it means you can go directly to that namespace like it is your current namespace
当您编写use namespace..
它时,这意味着您可以直接进入该名称空间,就像它是您当前的名称空间一样
Lets take a look at the next code:
让我们看看下一个代码:
We'll create a file and declare that it belongs to namespace classes\a
我们将创建一个文件并声明它属于命名空间 classes\a
//file: a.php
<?php
namespace classes\a;
class A{
}
now lets create another file b.php
(and declare it belongs to namespace classes\b but it means nothing for the example)
现在让我们创建另一个文件b.php
(并声明它属于命名空间 classes\b 但这对示例没有任何意义)
namespace classes\b;
require_once "a.php";
use classes\a; //Notice that I'm using this namespace, it means I can use it directly
class A{
}
Generates the error
产生错误
Fatal error: Cannot declare class classes\b\A because the name is already in use in
致命错误:无法声明类 classes\b\A,因为名称已在
We have to solutions possible:
我们必须有可能的解决方案:
First: remove the use
tag and write the namespace directly
第一:去掉use
tag,直接写namespace
class A{
function __constructor(){
$instance = new classes\a\A();
}
}
Second, give it alias
二、给它别名
use classes\a as out_a;
class A{
function __constructor(){
$instance = new out_a\A();
}
}
For your code, just remove the use
or give it an alias.
对于您的代码,只需删除use
或给它一个别名。
回答by ClementNerma
The problem is certainly because you include the RexBuilder.php
file two times instead of one.
问题肯定是因为您包含该RexBuilder.php
文件两次而不是一次。
If you call the file by this way : include('RexBuilder.php');
or this way require('RexBuilder.php');
please change it by include_once('RexBuilder.php');
or require_once('RexBuilder.php');
which only allows ONE call of the file.
如果您通过这种方式调用文件 :include('RexBuilder.php');
或这种方式require('RexBuilder.php');
请更改它include_once('RexBuilder.php');
或require_once('RexBuilder.php');
只允许对文件进行一次调用。