Is this a bad practice?
这是一个不好的做法吗?
like:
喜欢:
function boo(){
require_once("class.moo.php");
}
...
?
?
5 个解决方案
#1
7
Yes it is bad practice; no it's not.
是的,这是不好的做法;不,这不对。
You're likely going to get both answers, and here's why:
你可能会得到两个答案,这就是原因:
If you use __autoload
(or equivalent), calling:
如果您使用__autoload(或等效),请致电:
function someFunc()
{
$n = new UndefinedClassName();
}
is equivalent to:
相当于:
function someFunc()
{
include('path/to/UndefinedClassName.php');
//may be require_once, include_once, include, or require
//depending on how autoload is written
$n = new UndefinedClassName();
}
But you'll get better performance out of your code if you avoid using __autoload
. And to maintain your code, it's better to put all your includes
at the top of the script like you would for import
statements in other languages.
但是,如果您避免使用__autoload,您将从代码中获得更好的性能。为了维护您的代码,最好将所有包含放在脚本的顶部,就像使用其他语言的import语句一样。
include('path/to/UndefinedClassName.php');
...code...
function someFunc()
{
$n = new UndefinedClassName();
}
I would suggest consistency. If you consistently call the include
in the functions, you shouldn't have too many issues, but I would choose imports at the beginnings of files, or as autoloads
.
我会建议一致性。如果你一直在函数中调用include,你不应该有太多的问题,但我会选择文件开头的导入,或者作为自动加载。
#2
3
I would avoid it.
我会避免它。
It is not what another developer would expect, and, as such, would reduce the maintainability of your code.
这不是另一个开发人员所期望的,因此会降低代码的可维护性。
#3
1
This is how class loaders work. This is not necessarily bad practice.
这就是类加载器的工作方式。这不一定是不好的做法。
Depends on what the function does and why you are doing this. Using autoloading may be more appropriate.
取决于函数的功能以及执行此操作的原因。使用自动加载可能更合适。
#4
1
This is generally a bad practice and should be avoided. You should probably consider using an autoloader instead.
这通常是一种不好的做法,应该避免。您应该考虑使用自动加载器。
#5
0
If you have reasons for this I don't see anything bad about it.
如果你有理由我没有看到任何不好的事情。
#1
7
Yes it is bad practice; no it's not.
是的,这是不好的做法;不,这不对。
You're likely going to get both answers, and here's why:
你可能会得到两个答案,这就是原因:
If you use __autoload
(or equivalent), calling:
如果您使用__autoload(或等效),请致电:
function someFunc()
{
$n = new UndefinedClassName();
}
is equivalent to:
相当于:
function someFunc()
{
include('path/to/UndefinedClassName.php');
//may be require_once, include_once, include, or require
//depending on how autoload is written
$n = new UndefinedClassName();
}
But you'll get better performance out of your code if you avoid using __autoload
. And to maintain your code, it's better to put all your includes
at the top of the script like you would for import
statements in other languages.
但是,如果您避免使用__autoload,您将从代码中获得更好的性能。为了维护您的代码,最好将所有包含放在脚本的顶部,就像使用其他语言的import语句一样。
include('path/to/UndefinedClassName.php');
...code...
function someFunc()
{
$n = new UndefinedClassName();
}
I would suggest consistency. If you consistently call the include
in the functions, you shouldn't have too many issues, but I would choose imports at the beginnings of files, or as autoloads
.
我会建议一致性。如果你一直在函数中调用include,你不应该有太多的问题,但我会选择文件开头的导入,或者作为自动加载。
#2
3
I would avoid it.
我会避免它。
It is not what another developer would expect, and, as such, would reduce the maintainability of your code.
这不是另一个开发人员所期望的,因此会降低代码的可维护性。
#3
1
This is how class loaders work. This is not necessarily bad practice.
这就是类加载器的工作方式。这不一定是不好的做法。
Depends on what the function does and why you are doing this. Using autoloading may be more appropriate.
取决于函数的功能以及执行此操作的原因。使用自动加载可能更合适。
#4
1
This is generally a bad practice and should be avoided. You should probably consider using an autoloader instead.
这通常是一种不好的做法,应该避免。您应该考虑使用自动加载器。
#5
0
If you have reasons for this I don't see anything bad about it.
如果你有理由我没有看到任何不好的事情。