The task : to write an example script (or to describe an algorithm), which itself does nothing, but only connects the modules , and builds the whole system and these modules.

In PHP.

  • Well, for example: 1. Folder with modules 2. The script gets a list of files and directories 3. The script loops through the modules. And then the simplest: implement :) - zenith
  • @ Dima Faleleev, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - knes

1 answer 1

Yes Easy.

<?php $allowed_modules = array('user','catalog','pictures'); function __autoload($name){ if(in_array($name,$allowed_modules) && file_exists('/Modules/'.$name.'.php')){ require /Modules/'.$name.'.php'; }else{ header('HTTP/1.1 404 Not Found'); die('No such module'); } } 

as an option, the allowed_modules can also be obtained using the search module of available modules.

  • Somehow not quite in the topic $ allowed_modules and syntactically wrong ($ allowed_modules = array ('user', 'catalog', 'pictures');) and header ('HTTP / 1.1 404 Not Found'); die ('No such module'); All the more, there it is necessary to throw new Exception ('No such module') - Nord001
  • I stupid, sorry. Exception is a controversial issue. :) It still has to catch it, not a fact that it will. - knes
  • Well, at least autoload in no case should throw what you are doing now :) - Nord001
  • Should. Because otherwise, the standard error handler will be thrown for it, and this is much worse. Another thing is that I personally consider autoload not as a means of connecting modules, but as an entry point to emergency operation when other graceful degradation systems did not work anymore. Here we can manage to write a terrible inscription in the log and quietly die. But for the example of connecting modules, this is quite suitable. - knes
  • one
    @ Nord001, you can offer your own, correct, option in the answers to this question. No one will be offended, and perhaps even a plus. - knes