0

Below is my folder structure. I need to access the class inside a controller on the WordPress theme.

  • Controller->WP_Db_connection.php

  • Controller->WP_Product.php

  • samplePlugin.php

-> WP_Db_connection.php

class WP_Db_connection {

    public $dbConfig = [];
    public $errors = [];

    // Constructor
    function __construct() {
       self::init_db()
    }
    function init_db () {
       // Code to return DB connection
    }

}
new WP_Db_connection();

->WP_Product.php

require_once ROOT_PATH.'/WP_Db_connection.php';
class WP_Product{ 
   function __construct(){
     echo 'Inside Products';
   }
}
new WP_Product();

-> samplePlugin.php

spl_autoload_register ( "autoload", true, true );
function autoload($class){
    try{
        $classPath = ROOT_PATH.'/controllers/'.str_replace ( '\\', '/', $class ) . '.php';
        if($classPath && file_exists($classPath)){
            require_once $classPath;
        }
    } catch (Exception $e){
        echo '<br>Exception caught: '.$e;
    }
}
class WP_main {
  // Code for class
}
new WP_main();

in theme following code, WP_main exists but other class does not exist.

echo 'WP_main: '.class_exists('WP_main');
echo ',WP_Product: '.class_exists('WP_Product');
echo ',WP_Db_connection: '.class_exists('WP_Db_connection');

Output:

WP_main: 1,WP_Db_connection: ,WP_Db_connection:
  • unless classes are autoloaded, you need to require or include the relevant files – Mark Kaplun Feb 12 '18 at 14:53
  • Have a look at dependency injection, namespaces and autoloader. – guido Feb 12 '18 at 20:41
  • @guido & Mark Kaplun I got it, I can use the class using "use controller\WP_Product". But can I do it without mentioning this? In other plugins just by activating I am able to create an object for that class and use it in any theme. – Vishesh Madhusudhana Feb 13 '18 at 5:04
  • @VisheshMadhusudhana Nope, unless the class where you are trying to use controller\WP_Product is under the same namespace. But that's normal. Anyway you can not use any prefix or only one, for example MyNameSpace that is resolved under src/ and put every code in that directory but isn't the right choice. Namespaces are commonly used to fix requires and to organize the code in a better and consistent way. – guido Feb 14 '18 at 23:01
0

Relying on code which is in other plugins and themes is problematic. The major problem is with being sure that the plugin is actually active as otherwise your code is just going to break the site.

You can check that a class or function exists before using it, but if your functionality depends on their existence, what are you going to do in case they are not there?

The proper wordpress way of having a dependency relationship with another plugin is by hooking on actions and filters they supply, not by directly trying to access the code. Actually even if that code is available there is no promise that it will still be there in the next release of the plugin, while actions and filters are supposed to be stable.

This depends on the plugin to expose enough hooks and APIs to be useful for you, but if it doesn't you should not attempt to hack it by calling things directly, unless you understand that you are basically taking full responsibility for testing both plugins on every change you make

  • Yes, I get it !!! I have now changed to add_action and do_action. Thanks !!! – Vishesh Madhusudhana Feb 15 '18 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.