1

How to include a specific function based on requirement from a php file containing various function definitions.

eg.
PHP file functions.php contains 3 functions a(), b() and c()
I have to use require_once('/f/functions.php') in other.php file to use function c.

What could i do to only include the function c in file other.php file using require_once() function and avoid other functions not to be included ?

4
  • 3
    What will be the problem if the other functions (a & b) are included ? Commented Jul 31, 2017 at 10:54
  • 1
    No problem at all, i just want to avoid inclusion of unnecessary functions which are not required at the moment. Commented Jul 31, 2017 at 10:55
  • @xyz There is no built in way to do it. Any hacks will have a higher overhead than just including the functions. Commented Jul 31, 2017 at 10:56
  • In my opinion, a better approach would be to structure your code using namespaces and classes. Group functions to relevant classes and use autloloading to load the classes you need. Commented Jul 31, 2017 at 11:02

2 Answers 2

1

There is no built in way to do it. Although there are ways to go around it if you do it for the sport.

While this is highly not recommended. Really, just dont do it... here's how:

1) Read the file as text.

2) find the function block and extract it

3) run it through eval.

This will cause any global state set up in the file to not be run. Which you may or may not want.

SuperClosure does something similar I believe to serialize closures, which by default php does not support.

One could leverage the PHP7 abstract syntax tree to also includes related functions. For example, if there are three functions defined - A, B and C. A uses B internally. If you include A, you would want to also include B.

This wont be more performant than just including the whole file, but feel free to prove me wrong with benchmarks.


You might be interested in namespaces. Both classes and functions support it.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot, period. PHP doesn't have any sort of module system. To "include functions", all you can do is execute a source code file (what include will do), which will by the act of executing the code in the file define those additional functions, or anything else that's in the file. You cannot selectively pick only certain parts of the code to run.

5 Comments

Not quite true. It would be possible with manually parsing the file, extracting the function as text, and running it through eval. While highly not recommended (please dont), it is doable.
@Kaspars That is not something I would ever recommend doing, and it's also possible the code won't be executable if it defines relationships to other parts of the code in the same file.
@deceze Agree. :)
@deceze This is not codereview.
@Kaspars Meaning what exactly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.