Quick Tip: PSR-0 Namespaces in Sugar 7

Post originally written by Jesse.

If you want to use namespaces following the PSR-0 standard for your custom code, it is now possible with the SugarAutoLoader in sugar7. Now you don't have to insert require statements all over the place.
require_once('include/utils/autoloader.php');

SugarAutoLoader::addNamespace('Fbsg\\', 'custom/include/Fbsg');

Sugar's autoloader will now map requests for the class Fbsg\Example\Test to the file "custom/include/Fbsg/Example/Test.php".

Post From Faye Business Systems Group Director of Development Matthew Strickland

Contact Jesse Heller

Parents
  • Comment originally made by Radek.

    Hi,

    I think there is something wrong with the psr-0 autoloader. I followed your example and created a new namespace

    SugarAutoLoader::addNamespace('test\\', 'custom/test');

    then created a TestClass under custom/test/TestClass.php and did repair & rebuild.

    Autoloader failed to load it and after digging in the code I found that it tried to load the following file:

    custom/test/test/TestClass.php (which doesn't exist).

    Above example works well if I pass "psr4" as the 3rd parameter to the addNamespace function.

    I had the same problem with autoloading a vendor library called PHPExcel (downloaded via composer). I couldn't autoload it because the SugarAutoloader::getFileNamePsr0 method kept returning a wrong path to the file (same thing as in the first example).

  • Comment originally made by Matthew Marum.

    Hi Radek, what version of Sugar are you using?
Comment Children