Search code examples
phpnamespacespsr-4

Can I use a PHP library file of classes with PSR-4 autoloading, and does it even make logical sense?


I envisage developing an API client library which may use many small utility classes. Do I have to have a separate file for each class if using PSR-4? Is my approach sound, and if so how can I achieve it?

Edit in response to answers: I didn't understand the range of autoloading strategies in Composer particularly classmap autoloading and how that can better manage my own codebase.

Useful knowledge from Evert and Jeto.

Sample project layout:

src
  myproject
    client
      apilib.php

apilib.php includes:

class Connection {}
class Statistics {}
class SomeOtherThing {}

Or am I stuck with creating these files?

client/apilib/Connection.php
client/apilib/Statistics.php
client/apilib/SomeOtherThing.php

What namespaces will my classes use?


Solution

  • PSR-4 maps classes and namespaces to file and directory paths, but you are not required to follow PSR-4.