1

I am currently working as a student on php project which has grown since the beginning of time and has about 1800 php-files.

The problem is: it is completely without namespaces, or any of the PSR-4, etc. recommendations. The technical debt is strong with this one :).

We want to use composer (and twig and some libraries more) and having problems including this (especially composer). I think it's because of the overwrite of __autoload() via spl_autoload_register() in the composer-autoloader?

Is there a good and fast way to start integrating namespaces without rewriting the whole project?

neulaender
  • 173
  • 1
  • 3
  • 10
  • Ouch, no PSR-4? This project must be ancient. So I found a couple of resources for you [**here**](https://jtreminio.com/2012/10/composer-namespaces-in-5-minutes/) and [**here**](https://stackoverflow.com/questions/31504980/php-adding-custom-namespace-using-autoloader-from-composer). It's been ages since I used composer, so forgive me if these don't help too much. – Robert Dewitt Jun 21 '17 at 08:56
  • 2
    Note that namespacing doesn't have much to do with composer nor autoloading; those are all orthogonal concepts. – deceze Jun 21 '17 at 09:01
  • 1
    Composer can also deal with PSR-0 or manually specifying files and folders to autoload. It should be safe to get rid of any existing autoloaders and just let composer do its own thing. Check out https://getcomposer.org/doc/04-schema.md#autoload – apokryfos Jun 21 '17 at 09:09

1 Answers1

1

You can still use Composer with PSR-0 or classmap.

I'd probably go with the classmap first. Advantages: Can deal with multiple classes per file. Can deal with arbitrary file structures.

Once you achieved using Composer's autoloading, you can start removing either the existing autoloader or those require_once/include_once that are likely spread all over the place.

Once you've got rid of all that file loading legacy and have established Composer autoloading, you can try to organize the code according to PSR-0. This will probably require you to rename files and reposition them. It might also be the case that the classes don't have any identifiable prefixes - which is bad for your PSR-0 autoloading because all these files would belong into one single folder.

Note that until this point you haven't changed the name of any class, so the code should run without any changes.

Using namespaces does not have a very noticeable advantage. You'd be forced to rename all classes, rename all usages of that class name, and all in all it won't provide any noticeable benefit if used alone.

On the other hand, you sound like you do want to refactor everything else as well, so switching to namespaces can be used as a signal for "newer" code.

You can use PSR-4 and PSR-0 at the same time, so it won't affect your renaming of classes (besides the necessary class name changes in all the places).

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Thank you. I will try the links above (others answers and keep your recommendation in mind :). Especially that I/we can use PSR-4/0 at the same time is kind of obvious but new to me :). – neulaender Jun 23 '17 at 07:54