• Hi!
    I created class Includes.php in theme and created the same file in my plugin and I see only one file is included that that is belonged to plugin.

    How I can use the same classes in each plugin and each theme without rename them???

    This classes are using for create structure, and both have equal structure like this

    
    <?php 
    if(!class_exists('Includes')){
      class Includes{
        function __construct(){
          add_action( 'wp_enqueue_scripts', array(&$this, 'includeScripts') );
        }
    
        function includeScripts(){
          $this->includeCss();
          $this->includeJavaScripts();
        }
    
        function includeCss(){
          // include parent style
          wp_enqueue_style( 'style', get_template_directory_uri().'/public/css/style.css' );
        }
    
        function includeJavaScripts(){
          wp_deregister_script( 'jquery' );
          wp_enqueue_script( 'jquery', get_template_directory_uri().'/public/js/jquery-3.2.1.min.js', null, '3.2.1', false );
          
          wp_enqueue_script( 'public', get_template_directory_uri().'/public/js/public.js', array('jquery'), '1.0.0', true );
          // wp_enqueue_script( 'jquery' );
        }
      }//Includes
    
      new Includes();
    }
    ?>
    

    it is theme file, for plugin I use equal class only differ files and I’m going use the same classes for each next plugin, but it doesn’t work….
    Who know how to reuse created code?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter neo332

    (@neo332)

    I did it

    
    <?php 
    /**
    Plugin Name: ks-highlight-js
    Description: Description
    Plugin URI: https://kselax.ru
    Author: Author
    Author URI: https://kselax.ru
    Version: 1.0.0
    License: GPL2
    Text Domain: ks-highlight-js
    */
    namespace Ks;
    require_once dirname(__FILE__).'/core/class/KsHighlightJs.php';
    
    ?>
    

    this is didn’t help,

    only help when I directly put namespace ks; to Includes.php file, but I don’t what always reedit this file.

    Moderator bcworkz

    (@bcworkz)

    Yeah, you cannot declare the same class more than once in the same namespace. Plugins load first, so the theme’s re-declaration was ignored. Namespacing allows re-declaration because the class is no longer seen as the same.

    What is different between the versions that makes you want to namespace? Or are they identical and you wish to ensure it’s available even when your theme or your plugin are not both used? You could use class_exists() before including or declaring the class in each case. Then it will always be available without throwing redeclaration errors.

    If there is some difference, instead of namespacing, I suggest you create a base class that contains all the common elements. Then extend the class and override the methods that differ. Thus you end up with two different classes, but the bulk of each is declared only once.

    Another way to accommodate slight differences is to contain the differences in the same class and alter which version is used by passing arguments when instantiating. Then you have only one class, but two different objects that behave differently based on how the are instantiated.

    Thread Starter neo332

    (@neo332)

    I do in class namespace nameOfNamespace and use the same classes for all next classes, but I want to build my own framework for build wp plugins. It’s not useful use wp codex and always repeat to build the same code.

    I’m going to create libs from my own class that I will recopy in each plugins, something like this there exists wp crone, we create our class called WpCrone and put there our code, make incapsulationf ro existing wp functions and then we won’t google for repeat new code, simply we can read in file description of each our function and call it. It seems good idea.

    I have a few classes that I build but it not what I want, they are all mixed between reap wp functionality and mine, for each new project I reedit each file. Now want extract in folder wp-core all classes that will have only wp functionality and other classes new will extended from them. I think better use inheritance in perant classes will core in child changeable functionality.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use namespaces with wp plugins’ is closed to new replies.