16.08.11 - Add third party libraries to Symfony 2
How to add third party libraries to our Symfony 2 project
Adding libraries that follows the PHP 5.3.+ with namespaces is trivial, but how we can add an old PHP library without namespaces?
For example, if we want to add a library like GeSHi which haves an old style format (lower than 5.3.+), not follows the standards and it not have namespaces, we have to do a little trick.
As the Symfony 2 standard says, we have to store all the external libraries into the /vendor folder. In order to force GeSHi to load like the PSR-0 standard (fact standard for autoloading in Symfony 2) we must create the following directory structure:
And store there the GeSHi lib.
Now we have to make an special class to load GeSHi (class which inherits from real GeSHi class):
Now we can add safety the GeSHi lib to the autoloader:
Finally we can use it everywhere in our Symfony 2 project:
For example, if we want to add a library like GeSHi which haves an old style format (lower than 5.3.+), not follows the standards and it not have namespaces, we have to do a little trick.
As the Symfony 2 standard says, we have to store all the external libraries into the /vendor folder. In order to force GeSHi to load like the PSR-0 standard (fact standard for autoloading in Symfony 2) we must create the following directory structure:
/vendor/geshi/lib/Geshi/src
Now we have to make an special class to load GeSHi (class which inherits from real GeSHi class):
<?php
# vendor/geshi/lib/Geshi/Geshi.php
require_once __DIR__.'/src/geshi.php';
class Geshi_Geshi extends GeSHi {
}
# vendor/geshi/lib/Geshi/Geshi.php
require_once __DIR__.'/src/geshi.php';
class Geshi_Geshi extends GeSHi {
}
$geshi = new \Geshi_Geshi ();

Very interesting thank you. Do we have to extends every class of the library ??
Great! Simple and effective! thank you to explain in so simple way how to include external libraries!
Thanks for putting together this little tutorial. Saved me some hours for sure and works perfectly with the getId3 library (http://getid3.sourceforge.net).
I was looking for a way to solve this all morming. Thank you very much
This really helped me, was struggling with how to integrate a 3rd party PHP class into Symfony2. Followed your instructions exactly and it seems to work! Huge thanks!
i have problems to load a simple class cold sfEncrypt.php sfEncrypt { function myfunction() { ..... } ........ } The directory structure is vendor/sfEncrypt/lib/SfEncrypt/src the lib is under /src SfEncrypt/Sfencrypt.php: require_once __DIR__.'/src/sfEncrypt.php'; class SfEncrypt_SfEncrypt extends sfEncrypt { } in autoload.php $loader->registerPrefixes(array( 'SfEncrypt_ ' => __DIR__.'/../vendor/sfEncrypt/lib' )); in controller: $sfEncrypt = new \SfEncrypt_SfEncrypt(); i get: Fatal error: Class 'SfEncrypt_SfEncrypt' not found ????