Php 需要使用关键字

Php require use keyword

我有一个包含许多使用声明的 php 文件。

Use.php

<?php
    use 'class/MyClass1';
    use 'class/MyClass2';
?>

File1.php

<?php
    include_once 'Use.php'
?>

我想将它包含在另一个 php 文件中,但它似乎不起作用。

我必须在每个 php 文件上 copy/paste all Use 声明吗?

谢谢,

来自http://php.net/manual/en/language.namespaces.importing.php

Note: Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

所以您不能将 use 语句移动到一个单独的文件中,很遗憾。

如果您有多个文件具有大量相似的依赖关系,它可能表明您的代码需要重新组织,但那是一个单独的讨论。