PHP class autoloading

Some simple but great php tips from Codular.com today

Simple class autloading

<?php

function loadMyClass($class){
    echo 'We are loading class: ' . $class;
    include_once('classes/' . $class . '.inc.php');
    echo 'Class loaded.';
}

spl_autoload_register('loadMyClass');

In PHP 5.3 you can use the below

<?php

spl_autoload_register(function ($class){
        echo 'We are loading class: ' . $class;
        include_once('classes/' . $class . '.inc.php');
        echo 'Class loaded.';
 });

Just remove the echo sections before you deploy

Justin Kelly

Justin Kelly

Data Engineeer, Business Analytics, Web Developer, Library Technology specialising in PHP and Tableau

Based in Melbourne, Australia

Feel free to contact me justin@kelly.org.au or _justin_kelly