namespaces - php `use` class in parent folder -


i have installed propel orm composer cannot create new model php script not in same directory php class.

i have class test inside test.php , want use subfolder/index.php. note class test uses base/test base/test.php, using require() not option here base/test goes on use more classes generated composer.

traditionally, i'm supposed following:

<?php    use test; ?> 

but since have test in parent folder, cannot that, ,

<?php    use ../test; ?> 

doesn't work.

my folder structure:

my project |-- base |   `-- test.php <-- file referenced `test` class |-- subfolder |   `-- index.php <-- file want use `test` `-- test.php <-- file containing `test` class 

the actual code: subfolder/index.php:

<?php  use \test;     require __dir__ . '/../vendor/autoload.php';     $test = new test(); ?> 

test.php:

<?php use base\test basetest;  class test extends basetest {  } 

test namespace , has literally nothing folder structure. namespaces have folder-like structure, cannot use relative paths.

psr-4 autoloaders, such composer packages use these days, map namespaces in way very closely matches folder structure, still entirely separate concepts.

if have declared namespace in file subsequent names considered relative path. eg:

namespace foo;  class bar {}; // \foo\bar 

if want use outside of current namespace need declare full path, beginning \ signifies root namespace. eg:

namespace foo; use \test  class bar { // \foo\bar   public function test() {     $test = new test();     $dbh = new \pdo();   } } 

Comments