Php:当没有“先前声明”时如何解决“无法重新声明类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11186348/
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: how to resolve "Cannot redeclare class" when there's no "previously declared"
提问by Olivier Pons
I want to test the phpDocumentor-alpha, and there's a problem that some people seems notto have:
我想测试phpDocumentor-alpha,有一个问题似乎有些人没有:
# sudo pear uninstall phpdoc/phpDocumentor-alpha
uninstall ok: channel://pear.phpdoc.org/phpDocumentor-2.0.0a6
olivier@olivier-ubuntu ~/Documents/pizzas/dev # phpdoc --help
bash: /usr/bin/phpdoc: Aucun fichier ou dossier de ce type
#
# sudo pear install --alldeps -f phpdoc/phpDocumentor-alpha
downloading phpDocumentor-2.0.0a6.tgz ...
Starting to download phpDocumentor-2.0.0a6.tgz (1,107,853 bytes)
..................................done: 1,107,853 bytes
install ok: channel://pear.phpdoc.org/phpDocumentor-2.0.0a6
# phpdoc --help
PHP Fatal error: Cannot redeclare class phpDocumentor\Plugin\Core\Listener in /usr/share/php/phpDocumentor/src/phpDocumentor/Plugin/Core/Listener.php on line 194
Fatal error: Cannot redeclare class phpDocumentor\Plugin\Core\Listener in /usr/share/php/phpDocumentor/src/phpDocumentor/Plugin/Core/Listener.php on line 194
#
Ok, i can avoid that problem with:
好的,我可以通过以下方式避免这个问题:
if ( !class_exists('MTIHelperEstadosLocal') ) {...}
But this is just an ugly workaround. I'd like to know if there's a way to know where the declaration was firt (= which include or whatever).
但这只是一个丑陋的解决方法。我想知道是否有办法知道声明在哪里(= 包括或其他)。
Any idea?
任何的想法?
回答by Olivier Pons
Here's the simple solution:
这是简单的解决方案:
print_r(get_declared_classes());
回答by Ranieri Valen?a
In the file {path-to-pear-directory}/PEAR/phpDocumentor/vendor/composer/ClassLoader.php, the line 150 is:
在文件 {path-to-pear-directory}/PEAR/phpDocumentor/vendor/composer/ClassLoader.php 中,第 150 行是:
require $file;
symply change it to
symply 将其更改为
require_once $file;
回答by Bryan Hazelbaker
Depending on your OS, and how you have constructed the path to your document root, there may be an issue with capitals versus lower case letters in the path name. This often trips me up on large CMS projects such as drupal on Mac OSX
根据您的操作系统以及您构建文档根路径的方式,路径名称中的大写字母与小写字母可能存在问题。这经常让我在大型 CMS 项目上绊倒,例如 Mac OSX 上的 drupal
// Insert this debug code before require statement.
if (class_exists('classInQuestion')) {
$reflector = new ReflectionClass('classInQuestion');
echo $reflector->getFileName() . ' ' . $reflector->getStartLine() . "\n";
echo $newClassFile;
exit();
}
// Normal pre-existing require statement
require_once $newClassFile
回答by metzelder
In addition to all answers, what you might do is to also consider php declared classes. For example dotnet. When i explicitly define a class named Dotnet for purpose of using Dotnet binaries, it throws an error with the first instance creation of the class.
除了所有答案之外,您还可以考虑 php 声明的类。例如 dotnet。当我为了使用 Dotnet 二进制文件而显式定义一个名为 Dotnet 的类时,它会在创建该类的第一个实例时引发错误。
If you are to use your own modified class other than php presents to you, you should give them distinguishable names.
如果您要使用自己修改的类而不是 php 提供给您的类,您应该给它们提供可区分的名称。

