This question is independent but I did ask a similar question before:-
Composer Gives Error, "Class Not Found"
The problem was solved but I failed to explain the nesting issue. I thought it will be more appropriate to make a new question.
I searched a lot but I can't make the nesting namespaces to work with psr-4 autoloading.
Directory Structure:-
│ composer.json
│ run.php
│
├───src
│ ├───one
│ │ parentclass.php
│ │
│ └───two
│ childclass.php
│
└───vendor
│ autoload.php
│
└───composer
autoload_classmap.php
autoload_namespaces.php
autoload_psr4.php
autoload_real.php
ClassLoader.php
installed.json
LICENSE
parentclass.php:-
<?php
namespace myns\one;
abstract class parentclass
{
abstract public function abc();
}
childclass.php:-
namespace myns\two;
namespace myns\one;
use myns\one\parentclass as parentclass;
class childclass extends parentclass
{
public function abc()
{
echo 'hello world';
}
}
composer.json:-
{
"name": "myvendor/mypackage",
"description": "nothing",
"authors": [
{
"name": "Omar Tariq",
"email": "XXXXX@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"myns\\": "src/",
"myns\\one\\": "src/one/",
"myns\\two\\": "src/two/"
}
}
}
run.php:-
<?php
require_once __DIR__ . '/vendor/autoload.php';
use myns\two\childclass as childclass;
$childclass = new childclass();
$childclass->abc();
When I run php run.php
. It gives error:-
Fatal error: Class 'myns\two\childclass' not found in C:\wamp\...\run.php on line 7