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 :\
– Wallace Maxters
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?
– Erlon Charles
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)
– Wallace Maxters
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.
– Erlon Charles
Check if this link helps you: PHP import functions
– Don't Panic