Title: Base PHP Classes   Plugins   Sharing Classes and Organization?
Last modified: August 20, 2016

---

# Base PHP Classes Plugins Sharing Classes and Organization?

 *  [stvcg00](https://wordpress.org/support/users/stvcg00/)
 * (@stvcg00)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/)
 * Base PHP Classes + Plugins + Sharing Classes and Organization?
 * I have several SOAP classes and base php classes that I will need to share across
   plugins… What is the recommendation handling a PHP class library across multiple
   plug ins…

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [stvcg00](https://wordpress.org/support/users/stvcg00/)
 * (@stvcg00)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150341)
 * Uhg… Can any one help?
 *  Thread Starter [stvcg00](https://wordpress.org/support/users/stvcg00/)
 * (@stvcg00)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150342)
 * Please? anyone??…
    I am so frustrated with wordpress right now…
 *  [Kraignos](https://wordpress.org/support/users/kraignos/)
 * (@kraignos)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150343)
 * At first sight, two ways come to my mind:
    1. make a lib/ folder at the root of your wordpress install. Both of your plugins
       target this folder
 *  - pros : easy maintenance -> you edit your base classes and services one time
      for all your plugins
    - cons : a pain to re-use if you move your plugins to another wordpress
 *  -  make a lib folder for each plugin
 *  - pros : all the file the plugin needs are included in the same folder so, easy
      to move. allows differences in the base classes and services configuration
      for each plugin
    - cons : you have to update your base classes and services twice :/
 * I guess you already reached that conclusion, so I’m not really helping. Maybe
   someone would have better ideas…
 *  Thread Starter [stvcg00](https://wordpress.org/support/users/stvcg00/)
 * (@stvcg00)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150345)
 * Thank you for replying! =O) =O) =O) =O)
 * I greatly appreciate it! =O) =O) =O) =O)
 * Option 2 seems like the more times you load a library the more to would compound
   on performance because you are increasing the amount of code to be executed?
 * Also I noticed I have to name each class differently because it can not load 
   it multiple times or it generates and error…
 * I tried Option 1 in my theme but it doesn’t seem to share over to a plugin and
   theme but generates an error because the plugin will not see the class but the
   theme will and vice versa?
 *  [Kraignos](https://wordpress.org/support/users/kraignos/)
 * (@kraignos)
 * [15 years ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150360)
 * > Option 2 seems like the more times you load a library the more to would compound
   > on performance because you are increasing the amount of code to be executed?
 * No, because with opt’2 you want to make sure each plugin will load the lib **
   relative to his own** path and don’t even be aware of the lib/ folder in the 
   other plugin’s file structure. That’s why you can easily re-use this plug on 
   many projects. Easy to deploy !
    Assuming this file structure for example :
 * |_wp-content/
    ….|_plugins/ ……..|_mypluginNumberOne/ …………|__myPluginNumberOne.
   php (entry-point of the plugin) …………|_libs/ …………….|_myclass.class.php …………….|
   _foo.class.php …………….|_ … ……..|_myAwesomePlugPlug/ …………|_myAwesomePlugPlug.php(
   entry-point of the plugin) …………|_libs/ …………….|_myclass.class.php …………….|_somethingUseful.
   class.php …………….|_bar.class.php
 * You’ll have something like this in both entry-points scripts
    `<? require_once(
   dirname(__FILE__) . "/lib/myclass.class.php" ); ?>` But each plugin will load
   HIS own copy of “myclass.class.php”, located in HIS own lib/ folder. So there
   won’t be any conflict or file which is loaded twice. The server will load only
   files requested by the script.
 * > Also I noticed I have to name each class differently because it can not load
   > it multiple times or it generates and error…
 * Yep, you can’t call two vars/functions/classes, etc. with the same name otherwise
   it will raise a conflict. Or you’ll have to use _namespaces_ [http://www.php.net/manual/en/language.namespaces.php](http://www.php.net/manual/en/language.namespaces.php)
   
   But you can stick with avoiding duplicate names, it’s a lot easier ^^
 * > I tried Option 1 in my theme but it doesn’t seem to share over to a plugin 
   > and theme but generates an error because the plugin will not see the class 
   > but the theme will and vice versa?
 * Don’t have enough clues to tell you why it did not work for you, but it should.
 *  Thread Starter [stvcg00](https://wordpress.org/support/users/stvcg00/)
 * (@stvcg00)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150716)
 * I also noticed that you can load your library into the themes “functions.php”
   and the “functions.php” loads before the plugins load…
 * Therefore i can load all my based classes in this file and then extend customized
   plugins from “functions.php” It seems kind of unorganized but it seems to works…
 * It would be great if word press had to levels of customization…. the plugin folder
   and a global library folder the library folder would be a folder where one can
   upload base classes to where you could utilize these in your theme and plugins…
   this will help me reuse code.
 *  [MartyThornley](https://wordpress.org/support/users/martythornley/)
 * (@martythornley)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150728)
 * Try just adding your classes into “wp-content/mu-plugins/”, a little talked about
   feature. Anything in the “mu-plugins” as in “Must Use” gets loaded before all
   plugins. The only trick is that it won’t dig into folders, so all files need 
   to just sit in the “mu-plugins” directory or you need to have a main file, like“
   my-classes.php” then a directory with all your classes and use “my-classes.php
   to include them all.
 * This would include the classes before you load any plugins so they should all
   be available to any plugin.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Base PHP Classes Plugins Sharing Classes and Organization?’ is closed
to new replies.

 * 7 replies
 * 3 participants
 * Last reply from: [MartyThornley](https://wordpress.org/support/users/martythornley/)
 * Last activity: [14 years, 2 months ago](https://wordpress.org/support/topic/base-php-classes-plugins-sharing-classes-and-organization-1/#post-2150728)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
