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);
}
}
}
}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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.