Moodle – Code Styling for PHP

List of rules for proper development in the Moodle environment

Naming of parameters (do not use “_”  or uppercase)

//BED $Userid, $course_id, $ModuleCourseid
//GOOD $userid, $courseid, $modulecourseid,  $param1, $parm2 ..

Do not use global parameters to write data

global $data;

Do not use the required function to include class, must use namespaces

namespace core\event;
class base {

namespace mod_forum\event;
class post_read extends \core\event\base {

\mod_forum\event\post_read::create($post->id, ...)->trigger();

Name Space example in moodle

Always use get_string function for text or label text

$a = new stdClass();
$a->url = '';
get_string("languageidentifier',"plugin place",$a);

Use common Moodle function to generate URL path

$url = new \moodle_url('path_to_file',array("id"=>2));

Use common Moodle function to generates tags, links ,html

$attributes=array('class'=>'name of the classes');
html_writer::link($url, 'text',attributes)

Use the right syntax for access to MYSQL DB


$sql = "SELECT *
        FROM {some_table}
        WHERE id > :above";
$records = $DB->get_records_sql($sql, array('above'=>111));


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.