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

3

I see that in Python, we can import only one function from a given module, without having to load it all. In addition, this is great for avoiding role 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 though there are no native methods of importing just one function in PHP, is there any solution to this?

Or, should I always use the default below when I'm going to use functions in php?

if (! function_exists('y')) {

     function y($y){ return 'y'; }
}
    
asked by anonymous 07.07.2015 / 19:03

1 answer

2

Do not have this in PHP. But normally you do not create so many loose functions like this, you normally create classes and they contain functions / methods, etc. And to avoid naming conflicts with classes, there is a feature called namespaces.

    
07.07.2015 / 19:39