PHP导入功能 [英] PHP import functions

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

问题描述



假设我有一个名为的目录,

  array_select.func.php 
stat_mediam.func.php
stat_mean.func.php
.....

我想:加载每个单独的文件(里面定义了一个函数),并像使用内部php函数一样使用它,比如array_pop(),array_shift()等。

一旦我偶然发现了一个将用户定义函数编译为PHP安装的一部分的教程(现在我再也找不到它了)。虽然这不是一个很好的解决方案,因为在共享/经销商托管您无法重新编译PHP安装。



我不想与将来的PHP /其他扩展版本发生冲突,即如果一个名为X的函数是突然之间的内部PHP功能的一部分(尽管它可能不本身具有相同的功能)我不希望PHP因此而抛出致命错误,并且惨败。因此,我能想到的最好方法是检查函数是否被定义,使用function_exists(),如果是的话,抛出一个通知,以便在日志文件中很容易跟踪,否则定义该函数。然而,这可能会转化为在其他文件中有很多include / require语句,我需要这样一个函数,而我并不喜欢这样的函数。或者可能读取目录并遍历每个* .func.php文件和include_once。虽然我觉得这有点难看。



问题是,你有没有偶然发现过处理这种情况的一些源代码?它是如何实施的?你有没有做过类似的事情?我需要尽可能多的想法! :)

解决方案

有一种方法可以将这些功能放入类中,然后设置 __ autoload 功能。如果你反对在类中封装函数,这个解决方案可能不适用于你。我个人喜欢它,因为它允许我为我的函数命名空间并在它们之间共享私有方法。



首先,设置类似于此的自动加载函数。你会想调整命名约定来适应你自己的风格,并且可能会引入一些错误处理,但这只是为了得到基本思想。

  function __autoload($ class_name){
require_once(strtolower(library / $ class_name.class.php));
}

然后在代码中的任何地方,不管范围如何,都可以这样做。 / p>

  arrayFunctions :: doStuff($ myArray); 

PHP会自动尝试包含library / arrayFunctions.class.php并寻找一种称为arrayFunctions类中的doStuff。

I'm trying to find the best pragmatic approach to import functions on the fly... let me explain.

Say I have a directory called functions which has these files:

array_select.func.php
stat_mediam.func.php
stat_mean.func.php
.....

I would like to: load each individual file (which has a function defined inside) and use it just like an internal php function.. such as array_pop(), array_shift(), etc.

Once I stumbled on a tutorial (which I can't find again now) that compiled user defined functions as part of a PHP installation.. Although that's not a very good solution because on shared/reseller hosting you can't recompile the PHP installation.

I don't want to have conflicts with future versions of PHP / other extensions, i.e. if a function named X by me, is suddenly part of the internal php functions (even though it might not have the same functionality per se) I don't want PHP to throw a fatal error because of this and fail miserably.

So the best method that I can think of is to check if a function is defined, using function_exists(), if so throw a notice so that it's easy to track in the log files, otherwise define the function. However that will probably translate to having a lot of include/require statement in other files where I need such a function, which I don't really like. Or possibly, read the directory and loop over each *.func.php file and include_once. Though I find this a bit ugly.

The question is, have you ever stumbled upon some source code which handled such a case? How was it implemented? Did you ever do something similar? I need as much ideas as possible! :)

解决方案

One way you could pull something like this off is to put those functions into classes and then set up an __autoload function. If you are against wrapping the functions in classes than this solution probably won't apply to you. Personally I like it because it allows me to namespace my functions and share private methods between them.

First you set up your autoload function similar to this. You'll want to adjust the naming convention to fit your own style, and probably introduce some error handling, but this is just to get the basic idea across.

function __autoload($class_name){
     require_once(strtolower("library/$class_name.class.php"));
}

Then anywhere in your code regardless of scope you can do something like this.

arrayFunctions::doStuff($myArray);

PHP will automatically try to include "library/arrayFunctions.class.php" and look for a method called "doStuff" in the arrayFunctions class.

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

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