Managing Class Name Conflicts in Class Libraries

I've been creating plug-ins for some months now and I have developed a set of "base classes" (most are abstract classes but not all of them) that I use with the plug-ins (some are used with all plug-ins, others are used when a plug-in needs the specific thing the class addresses).

This, of course, works great BUT if I update one or more base classes to add some new functionality then if someone has more than one of my plug-ins installed it is very possible for one or more of those plug-ins to crash.

This is because if a plug-in using an older version of the updated class(es) load earlier then it "reserves" that name and the other than require the updated class don't have the functionality/fix/etc.

I've tried "nice" solutions but the only thing I can get to work reliably is to change the actual base class names for each plug-in which is far from ideal.

Namespaces obviously work but can't be used because so many popular hosting companies are using 5.2x versions of PHP.

I tried to dynamically create the class names and to use class factories but they didn't work or didn't overcome the need to rename all the base classes.

This can't be an unusual situation (do people really rewrite everything from scratch everytime and/or use distinct class names) but I can't find a solution and I can't think of an elegant one.

I looked at the WPMU DEV plug-ins but there doesn't seem to be any consistency between them, I assume because of all the different authors.

Do you have any solution to my problem?

  • aristath

    Hello there @DavidHusnian, I hope you're well today!

    Well, I can think of 2 ways to do this:

    Option 1: use a "commons" folder in your plugin where you will have each class as a separate file.
    Instead if simply including the files in your plugin, you could use something like this:

    if ( !class_exists( 'MyClass' )
      include_once FILENAME HERE

    Then, when you update your classes all you have to do in order to update your plugins is just overwrite with the new files.

    Option 2: Go modular.
    Now this might sound a bit weird, but it actually works better than you might think!
    Create a "base" plugin that in itself will do nothing. It will simply include all your classes and commons.
    Then, you can create all your other plugins as "modules" that simply plug-in to your base plugin and take advantage of all the great classes you're using.

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.