开发者

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 开发者_StackOverflow社区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


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.


I wouldn't use 'Option 1' mostly because it uses call_user_func* which will be slower that calling function directly.

If you only need some helper functions that doesn't really tie together, then I would, probably, include a file helpers.php which would just have a list of all my helper functions which are not encapsulated in static class since encapsulation doesn't really achieve anything in this case. Most of the times helper functions will be used for every request so including this file on every request won't cause any harm...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜