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?