Is it possible to autoload a file based on namespace in PHP?

Is it possible that is mentioned in the title? Python module style. See This Example, What I Definitely Mean.

index.php

<?php use Hello\World; World::greet(); 

Hello /World.php

 <?php namespace Hello\World; function greet() { echo 'Hello, World!'; } 

Is it possible?

+6
python php module namespaces autoload
source share
1 answer

Yes, look at the spl_autoload_register example

 namespace Foobar; class Foo { static public function test($name) { print '[['. $name .']]'; } } spl_autoload_register(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0 new InexistentClass; 

In the above example, something similar to:

 [[Foobar\InexistentClass]] Fatal error: Class 'Foobar\InexistentClass' not found in ... 

Here is a link to the autoloader , which

is a command line application to automate the process of creating an autoload file.

+5
source share

All Articles