Since PHP < 5.3 doesn’t offer namespacing, I’m wondering what the best way to approach this situation is w/regards to WordPress:
I wrote a plugin, using some helper classes from another, non-WP application of mine. The classes are pretty generic, for example one is named Tools. Another is named Pic, which is used to encapsulate all the functionality and data of an image. Additionally I have a base class from which all DB-related classes are derived. Altogether I have 5 classes, and as a plugin, it works great.
I then realized that I will write more plugins that would use these same helper classes. My options as I see them are:
-
have plugin #2 require plugin #1 to be installed, and use the same helper classes (that’s OK from my perspective but I’d like to make these plugins available to all w/o any such restrictions)
-
duplicate the helper functions into plugin #2 and give them different names (ie Plugin2_Tools, Plugin2_Pic, and similarly rename those in plugin #1 to Plugin1_Tools, etc…) I’m thinking this is the best way, though I could see having dozens of plugins all using the same base classes and helper classes and duplicating them would be a pain!
2a. I’m also thinking I should rename the classes and functions in any case so that they are more unique (EG MyPlugin_Tools….) so there is less chance of name duplication in the non-namespace PHP world.
Just curious if there is an established convention or best practice for this? I’m guessing #1 is good for private use and #2 is good for open source… Any suggestions appreciated.
For functions/classes you will be using across multiple functions you may want to consider appending your name or initials or company initials to the front. Then for plugin specific functions use a combination of the previous + the plugin name.
Example:
jrod_base_class;
jrod_laser_plugin_class;