Note that there are some explanatory texts on larger screens.

plurals
  1. POModule based system in PHP - how to?
    primarykey
    data
    text
    <p>I have a PHP web application where I've made a little module system, that basically works like this:</p> <p>Each module has it's own file, filename can be anything, but does not matter (for the sake of logic I named the filename is the same as the module name). The content of a module could look like this:</p> <pre><code>$MODULE["name"] = "some_module"; $MODULE["description"] = "This module does some things"; $MODULE["required"] = false; $MODULE["module_specific_var"] = some_function_that_returns_some_value(); </code></pre> <p>I have a global $MODULES array where I read all modules into:</p> <pre><code>foreach ($modules_list as $module_filename){ if (isset($MODULE)) unset($MODULE); include($module_path."/".$module_filename); $MODULES[$MODULE["name"]] = $MODULE; } </code></pre> <p>Now this is pretty simple and have been more than enough for my needs... Until now... Now I need a module specific function and that doesn't work with associative arrays (as far as I know of). What I'd like is something along the lines of being able to call <code>$MODULES["name_of_module"]["name_of_module_specific_function"]();</code></p> <p>I know this can be achieved with function references and as long as I use <code>include()</code> for each module, any functions defined in the module files will be available in global space - this may cause naming issues though, so bad idea.</p> <p>Alternatively I make each module a class, something along the lines of:</p> <pre><code>class module_name { var $name = "some_module"; var $description = "This module does some things"; var $required = false; var $module_specific_var = some_function_that_returns_some_value(); function module_specific_function(){ } } </code></pre> <p>Since I've already got a fair bit of code, my question is - is there an alternative to making all the modules class based, which avoids both the problem of name conflicts and allows me to keep the current module system fairly intact?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload