Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

Automatic class loading: Difference between revisions

From MoodleDocs
Line 30: Line 30:


==Namespaces==
==Namespaces==
==Backwards compatibility==
There are no know issues that could cause backwards compatibility, however it is strongly recommended to use the classes subdirectory only for classes that are included automatically.


==Performance and developer mode==
==Performance and developer mode==


Class autoloading improves performance and reduces memory footprint.
Class autoloading improves performance and reduces memory footprint. Location of all classes is automatically stored in class map cache, the cache is updated automatically before upgrade or installation, during cache reset and in developer mode.
 
Location of all classes is automatically stored in class map cache, the cache is updated automatically before upgrade or installation, during cache reset and in developer mode.


There is only one inconvenience - if you add new class or remove it you need to do one of the following:
There is only one inconvenience - if you add new class or remove it you need to do one of the following:
Line 41: Line 42:
* purge cache
* purge cache
* or add `$CFG->debug = (E_ALL | E_STRICT);` to your config.php
* or add `$CFG->debug = (E_ALL | E_STRICT);` to your config.php
Otherwise the new class would not be automatically loaded.


==See also==
==See also==

Revision as of 12:08, 8 June 2013

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.

Moodle 2.6


Class naming

All class names must start with Frankenstyle prefix such as mod_forum_:

Class file location

Examples

Example of autoloadated class in forum module:

<?php // file mod/forum/classes/some_class.php

class mod_forum_some_class {

}

<?php // file mod/forum/lib.php


$instance = new mod_forum_some_class();

Namespaces

Backwards compatibility

There are no know issues that could cause backwards compatibility, however it is strongly recommended to use the classes subdirectory only for classes that are included automatically.

Performance and developer mode

Class autoloading improves performance and reduces memory footprint. Location of all classes is automatically stored in class map cache, the cache is updated automatically before upgrade or installation, during cache reset and in developer mode.

There is only one inconvenience - if you add new class or remove it you need to do one of the following:

  • visit yourserver/admin/index/
  • purge cache
  • or add `$CFG->debug = (E_ALL | E_STRICT);` to your config.php

Otherwise the new class would not be automatically loaded.

See also