I am studying namespace in php and Doctrine 2.2 for a week.
I explorered several blogs and read several articles about namespace in php.
I understand that, when we want to use different namepaces in different php pages, we must write: include('php page that belong namespace we want to use');
But Doctrine 2.2 doesn’t use any include or require or require_once statements for using namespace. Almost all script in Doctrine 2.2 like that :
Doctrine\ORM\EntityManager.php
<?php
namespace Doctrine\ORM;
use Closure, Exception,
Doctrine\Common\EventManager,
Doctrine\Common\Persistence\ObjectManager,
Doctrine\DBAL\Connection,
Doctrine\DBAL\LockMode,
Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Mapping\ClassMetadataFactory,
Doctrine\ORM\Query\ResultSetMapping,
Doctrine\ORM\Proxy\ProxyFactory,
Doctrine\ORM\Query\FilterCollection;
class EntityManager implements ObjectManager
{
/**
* The used Configuration.
*
* @var \Doctrine\ORM\Configuration
.............
..................................
..................................
..................................
..................................
?>
There isn’t any include or require statement in Doctrine 2.2.
But we run the page (Doctrine\ORM\EntityManager.php) appear fatal error,
Fatal error: Interface ‘Doctrine\Common\Persistence\ObjectManager’ not found
in C:\xampp\htdocs\www\DoctrineExplained\DoctrineORM\Doctrine\ORM\EntityManager.php on line 45
Although Doctrine 2.2 is stable version of Doctrine ORM, why doesn’t use include or require for namespaces and uses?
Most PHP 5.3 code out there is like that. Doctrine expects autoload to be set-up before you start using it. You can define your own __autoload() function, use spl_register_autoload() or use one of the implementations bundled with many of the frameworks out there.
This could be done for a long time too. It just took time before the conventions were adapted and namespaces pushed for that change.