4

I want to test the phpDocumentor-alpha, and there's a problem that some people seems not to have:

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

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

4 Answers4

10

Here's the simple solution:

print_r(get_declared_classes());
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
3

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

// 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
2

In the file {path-to-pear-directory}/PEAR/phpDocumentor/vendor/composer/ClassLoader.php, the line 150 is:

require $file;

symply change it to

require_once $file;
  • 1
    This does not "fix" the problem, it just stops it from happening with a workaround. User should find the problematic code and fix it, such as using "require" twice is bad coding, and likely a bad codebase design or structure. This should be addressed properly. This suggestion is a bit like using `@` to "resolve" warning or notice messages. – James Sep 12 '15 at 20:11
0

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.

If you are to use your own modified class other than php presents to you, you should give them distinguishable names.

metzelder
  • 655
  • 2
  • 15
  • 35