Uploaded image for project: 'Moodle Community Sites'
  1. Moodle Community Sites
  2. MDLSITE-2261

Do not allow require_once at the top level of lib.php, and other frequently included files


    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Low Low
    • Coding style
    • None

      This is not exactly coding style, but it should be an aspect of the coding guidelines.

      I think the best practice rules are currenly these:

      1. lib.php should not require_once anything else. It is included from a lot of other places, and needs to be fast.

      2. If a particular function in lib.php needs to use other library code from the plugin to do a particular thing (e.g. cron) the put the necessary require_once inside the function, so that the include only happens when necessary.

      3. setting.php should no do any PHP queries when it is included. If you need to fetch options (or something) from the DB, ensure you only do that when the setting is going to be changed/displayed. admin_setting_configselect has a lazy loading mechanism for this.

      4. If you need library code in setting.php, the put that in a settingslib.php file, and require_once that. settingslib.php should not include anything else. (But see below.)

      5. If you have a lot of other library code in your plugin that should not be in lib.php, you have the following three options:
      a) make a locallib.php file with all the rest of your code.
      b) have a folder called lib (or classes - see below) and put your library code in there. Idealy the code should be object oriented, with one class per file.

      6. version.php and lang files should never require_once anything else. Ditto for things in db folder.


      A) I would now change 4. Given 1., and given that building the navigation already includes lib.php from most plugins, I think we should put library code required by settings.php in lib.php

      B) Both lib and classes subfolders have been proposed, and in some cases implemented. We really should make a decision and clearly recommend one or the other. (I used to prefer classes, but was convinced by the lib. I don't really care which, but clarity would be good).

      C) It occurs to me that once we have agreed the rules here, most of them could be verified by codechecker.

            poltawski Dan Poltawski
            timhunt Tim Hunt
            1 Vote for this issue
            9 Start watching this issue


                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.