I am pretty confused about the implementation of the namespace in php, especially when it comes to aliases - importing classes.
I read a tutorial from this lesson:
But I don’t understand - when used __autoload, why should I store the alias classes in folders, but when not__autoload used , the alias in the namespace is just fine, as shown 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);
?>
Until I get an error, if I did not store Conan.phpin the folderbarbarian
<?php
require_once "autoload.php";
use \barbarian\Conan as Cimmerian;
$conan = new Cimmerian();
var_dump($conan);
?>
error message
: require (barbarian/Conan.php): : C:\wamp\www\test\2013\php\namepace\autoload.php 12
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;
}
?>
? , , autoload?