Import namespace and files in PHP

I started building my code library with PHP 4. I used require_onceto import classes. Now with PHP 5.3, I come across the definition of namespaces and their import.

I want to change the source files to use importing ( usestatement) instead of using require_once. This is the right decision, I'm not sure.

I think that would be easy. Define the namespace at the top of the class files and search for and replace with other files that use them (replace require_oncewith use). In practice, what could go wrong?

And will there be a performance improvement? When you include a file, it is obvious that PHP finds the file. But when importing namespaces, this is not so obvious. Is there any performance loss when searching for namespaces and indexing them using PHP?

+5
source share
1 answer

useand require_once- completely different things. usedoes not import files at all. usejust simplifies your life. Instead of writing Full\Path\To\Classevery time, you can do

use Full\Path\To\Class

$bar = new Class();

You are still responsible for including the correct files.

Instead of downloading all the files manually, you can rely on loading a PHP autoclave .

Composer , Symfony 2 Zend2, .

use include_once . , , .

+16

All Articles