PHP import functions

I am trying to find a better pragmatic approach to importing functions on the fly ... let me explain.

Let's say I have a directory with a function name that has the following files:

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

I would like to: upload every single file (which has a function defined internally) and use it just like the php .. internal function, like array_pop (), array_shift (), etc.

As soon as I came across a tutorial (which I can no longer find now) that compiled user-defined functions as part of the PHP installation. Although this is not a good solution, because on the shared / reseller host you cannot recompile the PHP installation.

I do not want to have conflicts with future versions of PHP / other extensions, i.e. if a function called by me X suddenly becomes part of the internal php functions (although it may not have the same functionality as such) I do not want PHP to throw a fatal error because of this and fail.

So the best way I can come up with is to check if a function is defined using function_exists (), if it throws a notification so that it is easily tracked in the log files, otherwise define the function. However, this will probably translate into the fact that I have many include / require statements in other files where I need a function that I don't really like. Or perhaps read the directory and loop over each * .func.php and include_once file. Although I find it a little ugly.

: - - , ? ? - - ? !:)

+2
3

- - , __ autoload. , , , . , .

, . , , , , , , .

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

, , - .

arrayFunctions::doStuff($myArray);

PHP "library/arrayFunctions.class.php" , "doStuff" arrayFunctions.

+3

. .

, / ( ?) ( stat), ( ).

, .

0
  • To automatically upload files as needed, put your functions in classes and use autoload.
  • For name conflicts, use namespaces (if you have PHP 5.3).
0
source

Source: https://habr.com/ru/post/1788248/


All Articles