6

I am quite confused in implementing namespace in php, especially when it comes to alias - importing classes.

I have followed the tutorial from this tutorial:

But I don't understand - when __autoload is used, why I have to store the alias classes in folders, but when __autoload is not used, the alias in namespace are just fine, like this below,

<?php
namespace barbarian;
class Conan {
    var $bodyBuild = "extremely muscular";
    var $birthDate = 'before history';
    var $skill = 'fighting';
}
namespace obrien;
class Conan {
    var $bodyBuild = "very skinny";
    var $birthDate = '1963';
    var $skill = 'comedy';
}
use \barbarian\Conan as mother;
$conan = new mother();
var_dump($conan);
var_dump($conan->bodyBuild);

$conan = new \obrien\Conan();
var_dump($conan);
var_dump($conan->birthDate);
?>

While this I will get error, if I don't store Conan.php in the folder of barbarian

<?php
require_once "autoload.php"; 
use \barbarian\Conan as Cimmerian;
$conan = new Cimmerian();
var_dump($conan);
?>

the error message,

Warning: require(barbarian/Conan.php): failed to open stream: No such file or directory in C:\wamp\www\test\2013\php\namepsace\autoload.php on line 12

The autoload.php:

<?php
function __autoload($classname) {
  $classname = ltrim($classname, '\\');
  $filename  = '';
  $namespace = '';
  if ($lastnspos = strripos($classname, '\\')) {
    $namespace = substr($classname, 0, $lastnspos);
    $classname = substr($classname, $lastnspos + 1);
    $filename  = str_replace('\\', '/', $namespace) . '/';
  }
  $filename .= str_replace('_', '/', $classname) . '.php';
  require $filename;
}
?>

Is it a must to store alias classes in folders? Is it possible to import the classes without storing them in folders when autoload is used?

halfer
  • 19,824
  • 17
  • 99
  • 186
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

8

Autoloading classes with namespaces means it has to follow a convention, usually that convention involves using folders (compare with PSR-0).

If you have classes that sometimes follow that convention, then how would the autoloader know when to use folders or not?

So ultimately, yes classes should be stored in folders according to their namespaces. If you think the folder structure does not make sense, then you should change both namespaces and folder structure to reflect what you really want.

hakre
  • 193,403
  • 52
  • 435
  • 836
Populus
  • 7,470
  • 3
  • 38
  • 54
  • thanks for the answer. Now I understand the importance of folders in autoloading and aliasing. – Run Mar 28 '13 at 18:00
3

You don't have to use directories if you don't wish to - that example does, of course, but you can implement loading any way you wish. You could put all classes in a single folder, and use underscore characters in the form Classes/Barbarian_Conan.php. However that could get quite messy, and you'd have to work out what to do if you encountered an underscore in a class or namespace name!

That all said, it is a good idea to use directories this way. For maximum interoperability with other PHP software, furthermore, it is a good idea to follow PSR-0. More details about this standards group here.

As I discovered from the web, and you discovered from experimentation, neither the class name nor the namespace path names may contain a PHP reserved word.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Thanks. I should go for `directories`. The problem with my directory name is that I have a directory called `global` that keeps global classes and `global` is a keyword reserved in php! I must rename this directory to something else then? – Run Mar 28 '13 at 18:14
  • 1
    As far as I know that will be fine. Namespaces can contain reserved words as far as I know, [but classes cannot](http://stackoverflow.com/questions/11792070/php-reserved-words-as-namespaces-and-class-names). Edit: you were right, [see here](http://pornel.net/phpns). – halfer Mar 28 '13 at 18:18
  • nope. I will get an error for `use \global\Conan as Cimmerian;` – Run Mar 28 '13 at 18:25