Autoload classes with Zend based naming convention or no convention at all with Symfony 2

喜欢而已 提交于 2019-12-24 15:36:32

问题


How can I use the Symfony2 ClassLoader to autoload classes that dont follow PEAR naming conventions and don't use namespaces?

It seems from the symfony2 docs that this isn't possible. How does one include API helper libraries or other files that don't follow these types of conventions (i.e. they instead follow Zend conventions or no naming conventions at all)?


回答1:


You should register autoloading functions for those classes using spl_autoload_register(). This will let you have several autoloaders rune one after another, until one manages to load requied class.




回答2:


Code example of what Mchl said

in app/autoload.php

...
$loader->register();
...
/////////////////////////////////////////////////////////////////////////////////
// Own autoloader for non-namespaced nor PEAR lib. Replace tcpdf with yours.
function myLoader()
{
    $file = __DIR__ . '/../vendor/tcpdf/tcpdf.php';
    if (!file_exists($file))
    {
        return false;
    }
    require_once $file;
}

spl_autoload_register('myLoader');


来源:https://stackoverflow.com/questions/9253477/autoload-classes-with-zend-based-naming-convention-or-no-convention-at-all-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!