自动加载功能的最佳实践

自动加载功能的最佳实践

本文介绍了自动加载功能的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用PHP的 __ autoload()函数利用OOP范例的PHP项目时,以下哪一项被认为是独立管理的最佳实践功能:

提供的示例仅为了简洁而简化

还要注意,我已经发布了一个与 option 1 有关的参数/引用问题的相关问题,可以在这里找到:



给出 index.php

 函数__autoload($ class){
require $ class。code>:
.class.php;
}

选项1; 自动加载功能的摘要 Core 类(或 Library ,或其他)

  ## Core.class.php 
抽象类Core {
public static function __callStatic($ function,$ arguments ){
if(!function_exists($ function)){
require $ function。 .function.php;
}
返回call_user_func_array($ function,$ arguments);



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

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

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

选项2;将相关函数分组为抽象的helper类以静态调用:

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

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


解决方案

我不会使用'Option 1 '主要是因为它使用了call_user_func *,它将直接调用函数的速度变慢。



如果你只需要一些并不真正绑定在一起的帮助函数,可能包括一个helpers.php文件,它只包含所有我没有封装在静态类中的帮助函数的列表,因为封装在这种情况下并没有真正实现。大多数情况下,每个请求都会使用助手函数,因此在每个请求中包含此文件都不会造成任何伤害......


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)

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
解决方案

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

这篇关于自动加载功能的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:51