0

I am trying to write a small PHP class updater that can be used in multiple Wordpress plugins. The directory structure is something as given below:

Plugins
--pluginA
---main.php
---updater.php

--pluginB
---main.php
---updater.php

--pluginC
---main.php
---updater.php

updater.php

class updater {

}

main.php

require_once 'updater.php';
new updater();

But I always get a fatal error as there are multiple definitions of same class name exists.

My Approach #1

if ( ! class_exists( 'start' ) ):
class updater {

}
endif;

But I feel the above solution will not work because the plugin developer might have already a class name updater for some other special job.

My Approach #2 I tried with namespaces. It says undefined class because it tries to import the class from current namespace pluginA. Here the problem is, a plugin might have already a namespace as per the developer's convenience or its directory structure. And I don't want the developers to modify either their own namespaces nor the lib namespaces.

updater.php

namespace myLib;
class updater {
}

main.php

namespace pluginA;

require_once 'updater.php';
use myLib\updater;
new updater();

So how can I distribute my lib to all the developers who can just add a few lines to their main.php file and start using my library? Is the only solution to make the class name completely unique?

SkyRar
  • 1,107
  • 1
  • 20
  • 37
  • Does this answer your question? [3rd party dependency conflict in developing Wordpress Plugin](https://stackoverflow.com/questions/50144816/3rd-party-dependency-conflict-in-developing-wordpress-plugin) – rob006 Jan 06 '20 at 12:23

1 Answers1

0

First, you can't do this without modifying other plugin code unless you don't use classes at all.

The fastest way is not to use the same class name. If you insist on using the same class name, you must make good use of the namespace to separate it.

The excerpt PSR-4 specification 2-1 is as follows:

  1. The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.
  2. The fully qualified class name MAY have one or more sub-namespace names.

If you want to save the vendor name, I think it is ok, but at least keep the plugin name.

Take pluginA as an example, use myLib as the plugin name, and modifyupdater.php and main.php to:

updater.php

namespace myLib\pluginA;

class updater {
}

main.php

require_once 'updater.php';

namespace myLib\pluginA;

new updater(); // Full class name  is myLib\pluginA\update

Another plugin should do this, otherwise the name conflict situation will still exist.

Calos
  • 1,783
  • 19
  • 28
  • So you mean, when another developer use `updater.php` as a library in his plugin say `pluginB`, he should modify the namespace of `updater.php` to something like `namespace myLib\pluginB;` ? – SkyRar Jan 06 '20 at 02:16
  • @calos-kalo You mean somehow we have to make the class name or the namespace unique always. – SkyRar Jan 06 '20 at 02:25
  • 1
    Class names can be repeated, but they cannot be repeated in the same namespace. – Calos Jan 06 '20 at 02:30