We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Module and plugin 'dependencies'

Development and Programming

Paul Sijpkes's avatar
Paul Sijpkes
13 posts
10 years ago
Paul Sijpkes's avatar Paul Sijpkes

Is it possible to make a module dependent on another module?

For example if mymodule had a dependent module called dependent-on-mymodule, can its PHP code access any public variables set by mymodule?

So, for example:

{exp:mymodule param='1234'}

   {sometag1}
   {sometag2}

   {exp:dependent-on-mymodule}
          //uses internal PHP variables from mymodule

          {anothertag1}
   {/exp:dependent-on-mymodule}

{/exp:mymodule}

The reason I ask is because I currently have a module I use at work that is always growing and as it extends I’m creating dependent ‘modules’ that are loaded quite hackishly as template variables using eval(). In the example below I instantiate my_object as a means of accessing the ‘parent’ (my_object) from the dependant object (my_class).

It works nicely, but I’m sure there must be a more elegant way of doing this with EE’s existing template hierarchy.

// get $classes as comma separated template parameter
 if(!empty($classes)) {
           my_module ::$my_classes = explode(',', $classes); 
 }

 if(!empty(my_module::$my_classes)) {
// load the class from the library folder
            foreach (my_module::$my_classes as $class) {
                    $class = ucfirst($class);
                
                    ee()->load->library($class, array('my_object' => $this));
            }    
            
            $custom_libs = array();

// eval each class within the ee() application scope
             foreach (my_module::$my_classes as $class) {
                    $methods = get_class_methods($class);
          
                    foreach ($methods as $method) {
                       if($method !== "__construct") { 
                          $str_eval = "\$custom_libs['". $class . "_" . $method ."'] =  ee()->". $class . '->' . $method . "();";
                           eval($str_eval);
                       }
                    }
                }
        }
       
Paul Sijpkes's avatar
Paul Sijpkes
13 posts
10 years ago
Paul Sijpkes's avatar Paul Sijpkes

Ok, so it looks like no takers on this one. I’ve dug into my old comp-sci stuff and realised I can use a Singleton design pattern for this.

This means, basically, I define a static public variable in my ‘mymodule’ class called $instance and then pass this by reference to the Add-On or module or ‘dependent-on-mymodule’ class.

Just like EE does with the ee() super object.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.