Categories : Article, Symfony
Sometimes, Symfony presents unique problems with classes being silently redeclared. So much so that you might think that you are making changes to one file but your changes don’t have any effect.
Declaring a class in two separate files with that have the same filename, results in Symfony (Or similar frameworks) that have an autoloader to load only the first file that it gets to.
This is used by some programmers intentionally to override a class on purpose. For example, in a third party plugin where you cannot control the original class and it’s a challenge or it’s impossible to extend the third party plug in. That’s how this can be beneficial, but when you do not want to override it, it poses a challenge.
It can cause obstructions in a lot of situations. When you are deploying over a legacy FTP connection or rsync or gross, you might just end up with the same file in two different locations. Moving classes between deployments might end up loading classes twice and in two locations. Well, unless the entire code is removed from remote server, you will never realize that just one file is being used.
In large projects however, it sometimes happens that a class file gets duplicated and no one notices. Take for example, there are three developers working on similar platforms as well as PHP versions. They make changes to the class file that they perceive as “right” which turns out to be the file you expected to be loaded – not the duplicate one that people apparently forgot about. The duplicate file goes unnoticed by all developers – until some subtle differences in system configuration changes the order of the autoloaded files.
Here’s how your PHP class files should be named:
With this, you can run a report of duplicate class names
find . -name ‘*.class.php’ | xargs basename | sort | uniq –d
or in Mac OS, taking advantage of the native indexing:
mdfind -onlyin . -name ‘.class.php -actions.class.php -components.class.php’ | xargs basename | sort | uniq –d
After that, you can check each class for duplicate classes and determine which one you actually want to use and easily eliminate the ones that you know you aren’t going to use.