How To Make Composer (PSR-4) To Work With “.class.php” Extension?

时光总嘲笑我的痴心妄想 提交于 2020-01-04 05:25:20

问题


I've been facing a lot of problem with "Class Not Found" error. But recently I recognized the original problem. My previous two questions regarding the issue:-

Composer Gives Error, "Class Not Found"

PHP Composer PSR-4 Autoloading And Sub-Namespaces, Class Not Found

I'm interested in working with classes having extension class.php, e.g. parent.class.php and child.class.php. But composer doesn't seem to recognize these files and "Class Not Found" error is displayed. The problem is resolved if I use simple .php extension (child.php and parent.php).

I was wondering if it is possible to make composer work with .class.php extension for my php class files?


回答1:


If you want your code to be PSR-4 compliant, then you MUST have your filenames in the format classname.php.

From the PSR-4 spec:

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

(http://www.php-fig.org/psr/psr-4/)

So if you're sticking with PSR-4 then the answer is that you can't use your .class.php filenames. Just change the filenames and the problem will be solved.

If you absolutely have to keep the existing filenames for some reason, then you will need to work around it by writing your own autoloader function or just include them manually the old-school way. (But remember that you won't be conforming to PSR-4 if you do this)



来源:https://stackoverflow.com/questions/33298164/how-to-make-composer-psr-4-to-work-with-class-php-extension

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