How to autoload class with a different filename? PHP

喜夏-厌秋 提交于 2019-12-11 19:46:31

问题


I'd like to autoload a class, but it's declared inside a different class' filename like so:

AClass.php

namespace path\to\A;

class AClass {}

class AException extends \Exception {}

BClass.php

namespace path\to\B;

use path\to\A\AClass; // I'm actually after AException inside

class BClass {}

class BException extends AException {}

I'm not interested in loading AClass, but AException. However, the above code is not autoloading AException.


回答1:


Convention might dictate that the file name matches the class inside, but that's only convention. Your autoloader is welcome to use any mapping you find useful. You can always look for a class name of xxxxxException and map it to xxxxxClass.php



来源:https://stackoverflow.com/questions/23168921/how-to-autoload-class-with-a-different-filename-php

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