自动加载器功能 [英] Autoloader for functions

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

问题描述

上周我了解到,通过编写一个 __ autoload()函数,可以将类包含在项目中。然后我学会了使用自动加载器不仅是一种技术,而且是一种模式。

Last week I learned that classes can be included in your project by writing an __autoload() function. Then I learned that using an autoloader isn't only a technique but also a pattern.

现在我在我的项目中使用自动加载器,我发现它非常很有用。我想知道是否可能做同样的事情与功能。它可能是非常有用的,忘记包括正确的PHP文件与函数内。

Now I'm using the autoloader in my project and I've found it very very useful. I was wondering if it could be possible to do the same thing with functions. It could be very useful to forget about including the right PHP file with functions inside it.

那么,是否可以创建一个函数自动加载器?

So, is it possible to create a function autoloader?

推荐答案

没有函数的自动加载器功能。您有四个现实的解决方案:

There is no function auto-loader for functions. You have four realistic solutions:


  1. 将所有函数包装到命名空间类中所以,让我们假设你有一个函数 string_get_letters 。你可以将它添加到一个名为 StringFunctions 的类作为静态函数。因此,不是调用 string_get_letters(),您可以调用 StringFunctions :: get_letters()

  1. Wrap all functions into namespacing classes (context appropriate). So let's say you have a function called string_get_letters. You could add that to a class called StringFunctions as a static function. So instead of calling string_get_letters(), you'd call StringFunctions::get_letters(). You would then __autoload those namespaced classes.

预先加载所有函数。

加载函数在使用它们之前。在每个文件中, require_once 将在该文件中使用的函数文件。

Load functions prior to using them. In each file, require_once the function files that are going to be used in that file.

首先不要使用函数。如果你正在开发OOP代码(它似乎是你反正),应该有很少或根本不需要的功能。

Don't use functions in the first place. If you are developing OOP code (which it seems like you are anyway), there should be little to no need for functions at all. Everything you would need a function (or multiple) for, you could build in a OO manner and avoid the need for functions.

就个人而言,我建议您使用1,2或4,具体取决于您的确切需求以及代码库的质量和大小...

Personally, I'd suggest either 1, 2 or 4 depending on your exact need and the quality and size of your codebase...

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆