doctrine2 - PHP namespaces in project subfolders -


i've been struggling bit namespaces in php lately. beginning new project in object oriented fashion, want prepare project neat , clean structure functionnality.

as said, developped in object oriented php. using doctrine orm, global structure of project looks :

/admin   |- /index.php /css /fonts /img /js /src   |- /controller   |- /model   |- /system     |- /utils       |- /singleton.php   |- /view /vendor /index.php 

ok, let's take quick example.

i have singleton class lies in following path : src/system/utils/singleton.php

<?php  namespace system\utils;  /**  * base class singleton use cases  */ class singleton  {     /**      * returns *singleton* instance of class.      * @staticvar singleton $instance *singleton* instance of class.      * @return singleton *singleton* instance.      */     public static function getinstance() {         static $instance = null;         if ($instance === null) {             $instance = new static();         }         return $instance;     }     // private constructor, local construction allowed     protected function __construct() {     }      // private cloner, current instance      private function __clone() {     }      // private wakeup function, prevent unserializing     private function __wakeup() {     } } 

as can see, class uses namespace system\utils. then, in /index.php file in main folder, use class following :

<?php   require_once 'bootstrap.php';   use system\utils\singleton;    $singleton = singleton::getinstance();   var_dump($singleton); ?> 

(note : bootstrap.php configuration file doctrine. know uses kind of autoloading, , part of problem i'm exposing here, can find content of @ end of post)

this example works fine. great. but. if exact same test not in /index.php /admin/index.php, fails , php says can't find class singleton.

i'm getting confused here, because thought namespaces kind of "unique" identifier each class in project architecture , used retrieve 1 class anywhere in structure of project. obviously, wrong , googling around didn't me @ all.

my best guess goes towards autoloading , kind of stuff, since know doctrine uses it. maybe missed while configuring orm. said earlier, here content of /bootstrap.php containing configuration settings.

<?php // bootstrap.php use doctrine\orm\tools\setup; use doctrine\orm\entitymanager;  require_once "vendor/autoload.php";  // create simple "default" doctrine orm configuration annotations $isdevmode = true; $config = setup::createannotationmetadataconfiguration(array(__dir__."/src/model/entities/"), $isdevmode);  // database configuration parameters $conn = array(     'driver' => 'pdo_mysql',     'host' => 'host',     'port' => 'port',     'charset' => 'utf8',     'dbname' => 'database',     'user' => 'database',     'password' => 'database' );  // obtaining entity manager $entitymanager = entitymanager::create($conn, $config); 

i don't know if help, can use namespaces correctly anywhere inside /src folder seems doesn't work in subfolder of main directory.

if has clear explanation happens here , how namespaces should managed in php, , how apply corrections code, it'd nice of share knowledge.


edit :

okay has autoloading. managed working writing correct use statement namespace , declaring __autoload function requiring files once in ../src/.

<?php     use system\utils\singleton;      $singleton = singleton::getinstance();     $singleton->managesession();      function __autoload($classname)     {         echo "autoloading $classname.php in ../src/";         require_once("../src/$classname.php");     }      require_once '../bootsrap.php'; ?> 

this outputs autoloading system\utils\singleton.php in ../src/

so works , can continue solution. thing see workaround, not solution. still believe i'm doing wrong , optimized, can't figure out how... if have knowledge share on this, i'm ears!


thanks bunch in advance , sorry big post. if need kind of additional info, ask.

the problem you're doing same in admin/index.php, in case wrong.

the issue lies @ following line:

require_once 'bootstrap.php'; 

this tells php file bootstrap.php first relative file including/requiring file , if fails, in configured include paths.

it's not included in directory structure, looks of it, bootstrap.php sits @ same level index.php. fix issue replace above line in admin/index.php following:

require_once '../bootstrap.php'; 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -