PHP:无法声明类,因为名称已被使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42682501/
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
PHP: cannot declare class because the name is already in use
提问by egorik
I have 5 scripts, let's say: database.php, parent.php, child1.php, child2.php and somescript.php
我有 5 个脚本,比方说:database.php、parent.php、child1.php、child2.php 和 somescript.php
parent.php class looks like this:
parent.php 类如下所示:
include 'database.php';
class Parent {
public $db;
function __construct() {
$this->db = new Database();
}
}
child1 and child2 classes looks like this:
child1 和 child2 类如下所示:
include 'parent.php';
class Child1 extends Parent {
function __construct() {
parent::__construct();
}
function useDb() {
$this->db->some_db_operation();
}
}
The problem
问题
When I try to include both child1 and child2 in somescript.php, it returns the following error:
当我尝试在 somescript.php 中同时包含 child1 和 child2 时,它返回以下错误:
cannot declare class Database because the name is already in use in database.php on line 4 (this is the line which contains words 'class Database')
无法声明类数据库,因为该名称已在第 4 行的 database.php 中使用(这是包含单词“类数据库”的行)
But if I include only a single file (child1 or child2), it works great.
但是如果我只包含一个文件(child1 或 child2),它就很好用。
How do I correct that?
我该如何纠正?
回答by M31
you want to use include_once()or require_once(). The other option would be to create an additional file with all your class includes in the correct order so they don't need to call includes themselves:
您想使用include_once()或require_once()。另一种选择是创建一个附加文件,其中包含所有类以正确的顺序包含,这样他们就不需要自己调用包含:
"classes.php"
“类.php”
include 'database.php';
include 'parent.php';
include 'child1.php';
include 'child2.php';
Then you just need:
那么你只需要:
require_once('classes.php');
回答by Solaymane Chamane
try to use use include_once
or require_once
instead of include
or require
尝试使用include_once
或require_once
代替include
或require
回答by Fergal Andrews
Another option to include_once or require_once is to use class autoloading. http://php.net/manual/en/language.oop5.autoload.php
include_once 或 require_once 的另一个选项是使用类自动加载。http://php.net/manual/en/language.oop5.autoload.php
回答by pram
You should use require_once and include_once. Inside parent.php use
您应该使用 require_once 和 include_once。在parent.php里面使用
include_once 'database.php';
And inside child1.php and child2.php use
而在 child1.php 和 child2.php 里面使用
include_once 'parent.php';
回答by gentle
Class Parent cannot be declared because it is PHP reserved keyword so in effect it's already in use
无法声明父类,因为它是 PHP 保留关键字,因此实际上它已在使用中
回答by PlanetCloud
I had this problem before and to fix this, Just make sure :
我之前遇到过这个问题并要解决这个问题,请确保:
- You did not create an instance of this class before
- If you call this from a class method, make sure the __destruct is set on the class you called from.
- 您之前没有创建此类的实例
- 如果您从类方法调用此方法,请确保在您调用的类上设置了 __destruct。
My problem (before) :
I had class : Core, Router, Permissions and Render
Core include's the Router class, Router then calls Permissions class, then Router __destruct calls the Render class and the error "Cannot declare class because the name is already in use" appeared.
我的问题(之前):
我有类:Core、Router、Permissions 和 Render Core 包含 Router 类,Router 然后调用 Permissions 类,然后 Router __destruct 调用 Render 类和错误“无法声明类,因为名称已在使用中”出现了。
Solution :
I added __destruct on Permission class and the __destruct was empty and it's fixed...
解决方案:
我在 Permission 类上添加了 __destruct 并且 __destruct 为空并且已修复...