I am developing a project with the following structure:
Thegoalistomakeautoloadloadtheclasses.
Theautoload.phpfile:
functionautoload($className){$className=ltrim($className,'\');$fileName='';$namespace='';if($lastNsPos=strrpos($className,'\')){$namespace=substr($className,0,$lastNsPos);$className=substr($className,$lastNsPos+1);$fileName=str_replace('\',DIRECTORY_SEPARATOR,$namespace).DIRECTORY_SEPARATOR;}$fileName.=str_replace('_',DIRECTORY_SEPARATOR,$className).'.php';require$fileName;}spl_autoload_register('autoload');
AbstractPersonClass
namespaceProjeto\Cliente;useProjeto\Cliente\Interfaces\ClienteEnderecoInterface;useProjeto\Cliente\Interfaces\ClienteImportanciaInterface;abstractclassPessoaimplementsClienteEnderecoInterface,ClienteImportanciaInterface{}
ClientEnderecoInterfaceInterface
namespaceProjeto\Cliente\Interfaces;interfaceClienteEnderecoInterface{}
InterfaceImportanceInterface
namespaceProjeto\Cliente\Interfaces;interfaceClienteImportanciaInterface{}
ClientePFClass
namespaceProjeto\Cliente\Tipos;useProjeto\Cliente\Pessoa;classClientePFextendsPessoa{}
ClientePJclass
namespaceProjeto\Cliente\Tipos;useProjeto\Cliente\Pessoa;classClientePJextendsPessoa{}
Index.php
require_once('./inc/autoload.php');$cli=newClientePF();$cli2=newClientePJ()
Whentryingtoloadtheindex,phptellsthesystemthatitcannotfindtheclasses:
(!)Warning:require(ClientePF.php):failedtoopenstream:NosuchfileordirectoryinC:\wamp\www\inc\autoload.phponline15CallStack#TimeMemoryFunctionLocation10.0010152944{main}()...\index.php:020.0030157944spl_autoload_call()...\index.php:3330.0030158000autoload()...\index.php:33
Whatshouldbethecorrectwaytodeclarenamespacesandautoload?FullCodecanbedownloadedhere: GitHub Given the existence of psr-4, I can use it in place of psr-0