In PHP, is there any way to import only one function from a given file?

Asked

Viewed 397 times

4

I see that in Python, we can import only one function from a particular module, without having to load it all. In addition to this being great to avoid function name conflicts.

Example:

#funcs.py

def x(x):
    return 'x'

def y(y):
    return 'y'


#main.py

from funcs import y

print(y()); #y
print(x()); #erro é gerado

However, in PHP, when we have the same scenario, we have:

#func.php

function x($x)
{
    return 'x';
}

function y($y)
{
     return 'y';
}

#main.php

include_once 'funcs.php';

echo y(); // y
echo x(); // x

Even knowing that there is no native means of importing only one function, in PHP, there is some solution for this?

Or really, I should always use the default below when using php functions?

if (! function_exists('y')) {

     function y($y){ return 'y'; }
}
  • I’m asking this because it doesn’t seem like a good idea to separate each function in a file :\

  • Your question is very interesting for a discussion, but I have a question about it, because you would want to use only a file function?

  • It is because I do not like having to create a function file always having to declare the functions as in the last example... And also, every function that is loaded is more memory (I don’t know if this generates an absurd consumption, but in fact it generates some consumption having declared functions, however I’m not using)

  • In php it is not a big problem to have, for example, a library with 200 functions, as long as each one is well written. This will not bring you a loss in software performance large enough to make a noticeable difference to the end user. The way php and python work is different, so this is not such a big concern to have in php.

  • Check if this link helps you: PHP import functions

1 answer

3

You don’t have that in PHP. But normally you don’t create as many loose functions as this, usually you create classes and they contain functions/methods, etc. And to avoid conflict of classifications with classes, there is a feature called namespaces.

  • Friend, what would be "loose functions"? Because a class is not a repository of functions. Namespaces solve name conflicts yes, but I want to load a function from a file that contains 25 declared functions?

  • 2

    I meant that it’s unusual to create a file with 25 functions that have nothing in common with each other. For example, if you happen to have 25 mathematical operations functions, create a class called Calculus and group, something like that. There is nothing wrong with that. But that you want to include only a function of a file with 25, unfortunately there is no such thing in php =/

  • @Wallacemaxters to me and from what I read your problem lies in how to approach the subject. You have already thought of classes. The path in PHP is OOP. I develop large projects and an archive with so many functions no longer used for years.

  • I understand, @chambelix!. I only program PHP with OOP too. But this is a curiosity that came up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.