特征,包含功能的面向对象方式?
Traits, OO way of including functions?
除了 include/require
的面向对象方式之外,我真的想不出任何特征。
如果我想要一组与任何特定 class 无关的函数,或者是非常常见的可以与多个 class 一起使用的函数,我将它们放在 php
中文件和 include
或 require
它在 class 我想在其中使用它们。
include '/path/to/a/file.php';
有了 traits,我可以做类似的事情:
trait Common {
// common functions
}
并在 class 和 use Common;
中使用它
所以我是对的,将特征视为古老的面向对象方式include/require
?
根据 PHP 文档,http://php.net/manual/en/function.include.php include 导致函数和 classes 的范围是全局的,而特征仅在 class 范围内。这可能是最大的区别。
除了 include/require
的面向对象方式之外,我真的想不出任何特征。
如果我想要一组与任何特定 class 无关的函数,或者是非常常见的可以与多个 class 一起使用的函数,我将它们放在 php
中文件和 include
或 require
它在 class 我想在其中使用它们。
include '/path/to/a/file.php';
有了 traits,我可以做类似的事情:
trait Common {
// common functions
}
并在 class 和 use Common;
所以我是对的,将特征视为古老的面向对象方式include/require
?
根据 PHP 文档,http://php.net/manual/en/function.include.php include 导致函数和 classes 的范围是全局的,而特征仅在 class 范围内。这可能是最大的区别。