General developer forum

Using locallib.php vs using classes

 
Richard Jones
Using locallib.php vs using classes
Particularly helpful MoodlersPlugin developersTesters

Hi all

I'm sure I read somewhere that developers should not use locallib.php anymore but rather prefer using classes for local functions.  

Eg if I want to set some options for display of an item I could add that to, say, myplugin/classes/displayoptions.php or some suitable sub-folder.

However, in example code I see locallib.php used frequently.

Any advice on when to use which would be welcome.

 
Average of ratings: -
Davo
Re: Using locallib.php vs using classes
Core developersParticularly helpful MoodlersPlugin developers

If you're working with old, legacy code, then continue to use the existing locallib.php, unless you've got time to refactor it properly.

If you're writing anything new, you there is no reason to be creating a local locallib.php file.

 
Average of ratings: Useful (3)
Richard Jones
Re: Using locallib.php vs using classes
Particularly helpful MoodlersPlugin developersTesters

Thank you.

 
Average of ratings: -
Picture of Dave Balch
Re: Using locallib.php vs using classes
Core developersParticularly helpful MoodlersPlugin developers

I hadn't realised that was the case - the dev docs seem pretty scattershot. Is this page on automatic class loading the current info?

 
Average of ratings: Useful (1)
Picture of Mark Johnson
Re: Using locallib.php vs using classes
Core developersParticularly helpful MoodlersPlugin developers

That's a good point. I believe that page is the current info.

A quick search of the wiki for "locallib.php" suggests that these pages need updating to reflect current best practice:

https://docs.moodle.org/dev/lib.php
https://docs.moodle.org/dev/Libraries_Organization
https://docs.moodle.org/dev/NEWMODULE_Documentation

I'll do a quick update of these page while I'm at it, any improvements on what I write are welcome!

 
Average of ratings: -
Richard Jones
Re: Using locallib.php vs using classes
Particularly helpful MoodlersPlugin developersTesters

This might be something that could be added?

I have data handling functions and utilities for my sub-tables of an activity module (like saving and updating them). I want to put these in functions (classes?) if I feel they are too complex or long to go in the editing page. 

Are there naming conventions like all db handling methods go into one sub-folder (or page) and utilities in another? 

Or is that up to the individual developer just to use common sense?

 
Average of ratings: -
Davo
Re: Using locallib.php vs using classes
Core developersParticularly helpful MoodlersPlugin developers

If you are going to use a subfolder, then, to avoid clashes with future core Moodle class namespaces (e.g. the 'task' and 'event' subfolders), you should create your subfolders inside 'local', e.g. myplugin/classes/local/blah - after that, you're free to do what you want (and note that the use of 'local' is not enforced, just encouraged).

 
Average of ratings: Useful (1)
Picture of David Pesce
Re: Using locallib.php vs using classes
Plugin developers

Any idea when the example github repo (https://github.com/moodlehq/moodle-mod_newmodule) will be updated to reflect how plugins should be developed?


I'd like to start working on an xapi/cmi5 activity module and am getting a bit lost in the Moodle docs.


P.S. I've got a background in documentation. If someone can point me to scratch notes and/or provide a tutorial via webconference (I'll pay), I can update the docs.

 
Average of ratings: Useful (1)