WordPress.org

Ready to get started?Download WordPress

Forums

Base PHP Classes + Plugins + Sharing Classes and Organization? (8 posts)

  1. stvcg00
    Member
    Posted 3 years ago #

    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...

  2. stvcg00
    Member
    Posted 3 years ago #

    Uhg... Can any one help?

  3. stvcg00
    Member
    Posted 3 years ago #

    Please? anyone??...
    I am so frustrated with wordpress right now...

  4. Kraignos
    Member
    Posted 3 years ago #

    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
    2. 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...

  5. stvcg00
    Member
    Posted 3 years ago #

    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?

  6. Kraignos
    Member
    Posted 3 years ago #

    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
    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.

  7. stvcg00
    Member
    Posted 2 years ago #

    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.

  8. MartyThornley
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.