5

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?

Community
  • 1
  • 1
Omar Tariq
  • 7,536
  • 10
  • 38
  • 57
  • 2
    That's how [PSR-4 is meant to be](http://www.php-fig.org/psr/psr-4/): *The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.* – Álvaro González Oct 23 '15 at 08:43
  • 1
    I suggest simply not to use those extensions. It's legacy convention from PHP4. No one uses this style anymore. Nowadays, you can recognize a file holding a class by it's first letter beeing uppercase. – Daniel W. Oct 23 '15 at 08:43
  • @DanFromGermany If I've to upgrade my knowledge to latest conventions and standards of PHP then where should I look for? – Omar Tariq Oct 23 '15 at 08:52
  • The standards recommendation in PHP is hosted at http://www.php-fig.org/ - note that this isn't the only effort to standardize things, nor the only way to start proposing standards. – Sven Oct 28 '15 at 23:07

1 Answers1

4

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)

Simba
  • 4,952
  • 3
  • 19
  • 29