Autoloading functions best practices

蓝咒 提交于 2019-12-07 14:42:20

问题


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

回答1:


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...




回答2:


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.


来源:https://stackoverflow.com/questions/5600883/autoloading-functions-best-practices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!