John smith John smith - 6 months ago 31
PHP Question

Php require use keyword

I have a php file with many use declarations.

Use.php

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


File1.php

<?php
include_once 'Use.php'
?>


I would like to include it on another php file but it doesn't seem to work.

Must I need to copy/paste all Use declaration on every php file ?

Thanks,

Answer

From 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.

So you can't move use statements into a separate file, unfortunately.

If you've got multiple files that have a large set of similar dependencies, it could be a sign that your code needs reorganising, but that's a separate discussion.

Comments