问题
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