Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoloading functions best practices

When working on a PHP project that takes advantage of the OOP paradigm with PHP's __autoload() function, which of the following is considered the best practice for managing stand-alone functions:

(Examples provided are simplified for the sake of brevity)

tl;dr: How is stand-alone function loading commonly handled:

  • pseudo-autoloading (via __callStatic magic for example) [Option 1]
  • abstract helper class grouped static methods [Option 2]
  • an alternative

Also note, I've posted a related question regarding a parameter/reference issue with option 1, which can be found here: __callStatic(), call_user_func_array(), references, and PHP 5.3.1

Given in index.php:

function __autoload($class){
    require $class . '.class.php';
}

Option 1; An abstract Core class (or Library, or whatever) that "autoloads" functions:

## Core.class.php
abstract class Core{
    public static function __callStatic($function, $arguments){
        if(!function_exists($function)){
            require $function . '.function.php';
        }
        return call_user_func_array($function, $arguments);
    }
}

## SayHello.function.php
function sayHello(){
    echo 'Hello';
}

## SayGoodbye.function.php
function sayGoodbye(){
    echo 'Goodbye';
}

## index.php
Core::sayHello();   // Hello
Core::sayGoodbye(); // Goodbye

Option 2; Grouping related functions into abstract "helper" classes to be called statically:

## SayStuff.class.php
abstract class SayStuff{
    public static function sayHello(){
        echo 'Hello';
    }
    public static function sayGoodbye(){
        echo 'Goodbye';
    }
}

## index.php
SayStuff::sayHello();   // Hello
SayStuff::sayGoodbye(); // Goodbye
like image 844
Dan Lugg Avatar asked May 29 '26 22:05

Dan Lugg


1 Answers

I don't see a precise question here, but since the title contains "best practices", here's a piece of advice that I've recently discovered:

It seems to be preferable to use PHP's SPL autoload functions instead of __autoload() (nice example on "Dissection by David" blog).

Quoting the linked blog post:

The biggest benefits of using the SPL version (that I can see to-date) are:

  • more than one function can be used/registered — functions are chained together and used sequentially up to the point of one function loading the class file. + functions can be unregistered on-the-fly, too.
  • there is some nice error handling that can be implemented (see example 3), although some hat try/catch so this may not fit your coding style.
  • using a different extension (i.e. not .php or .php.inc or .inc) if you so choose with spl_autoload_extensions()
  • makes sure that ‘my’ autoloading class is not overwritten! If __autoload() is run later, my spl_autoload_register()’ed functions will not be replaced.
like image 99
Frosty Z Avatar answered Jun 01 '26 13:06

Frosty Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!