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

Web Developer, Business Analytics, Data Engineer specialising in PHP and Tableau

Based in Melbourne, Australia

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

Leave a comment

Name Notify me of replies by email.