# Tips

# array_column() vs foreach()

Use array_column() instead of foreach() to save CPU and line of codes

    // Use array_column()
    $records = $DB->get_records_sql("SELECT distinct u.id FROM {$sql->from} WHERE {$sql->where}", $sql->params);
    $userids = array_column($records, 'id');

    // Instead of foreach()
    $records = $DB->get_records_sql("SELECT distinct u.id FROM {$sql->from} WHERE {$sql->where}", $sql->params);
    foreach ($records as $record) {
        $userids[] = $record['id'];
    }

# Moodle Install Languages

Add language pack following the instructions on moodle language install pack, i.e. donwload a language pack and paste in <moodle codebase>/ lang
And then make sure
It is also necessary to add the folder found in /var/www/tpk-testing-totara/lang/mi_wwow it to the site-data directory: /var/lib/sitedata/tpk-testing-totara/lang/mi_wwow in order for it to install correctly. For more info visitELearning/Clients/Te Puni Kokiri
cp -r /var/www/victoria-prod-moodle/current/lang/mi_wwow /var/lib/sitedata/victoria-prod-moodle/lang/mi_wwow

# Debugging

Use mtrace to print debugging output on the terminal. You further user json_encode for it to print in a more user friendly mode.
mtrace(json_encode($mycode));

# Inheritance

How to import from parent class

// The parent class.
class myparent {
    private $abc;
    private $foobar;

    public function __construct($abc) {
        $this->abc = $abc;
        // Do stuff
    }

    public function get_stuff() {
        // Getting stuff
    }
}

// Another file which imports from myparent.
use myparent;
class mychild {
    private $xyz;
    // Access variable from parent class by defining it again.
    private $foobar;

    public function __construct(myparent $xyz) {
        $this->xyz = $xyz;
        $this->foobar = $foobar;
        // Access function from parent class.
        $mystuff = $xyz->get_stuff();
        // Do stuff
    }
}