I'm using Windows 10. After making a folder src
in the root directory I created two files in it.
Directory Structure (Before running composer install
):
│
├── composer.json
├── run.php
│
└── src
├── childclass.php
└── parentclass.php
Two files in the root directory:
composer.json:
{
"name": "myvendor/mypackage",
"description": "nothing",
"authors": [
{
"name": "Omar Tariq",
"email": "[email protected]"
}
],
"require": {},
"autoload": {
"psr-4": {
"myns\\": "src/"
}
}
}
run.php:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use myns\childclass as childclass;
$childclass = new childclass();
$childclass->abc();
Two files in the src folder:
childclass.php:
<?php
require_once 'parentclass.php';
use myns\parentclass as parentclass;
class childclass extends parentclass
{
public function abc()
{
echo 'hello world';
}
}
parentclass.php:
<?php
namespace myns;
abstract class parentclass
{
abstract public function abc();
}
Directory structure after running composer install
:
│
├── composer.json
├── run.php
│
├── src
│ ├── childclass.php
│ └── parentclass.php
│
└── vendor
├── autoload.php
│
└── composer
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── ClassLoader.php
├── installed.json
└── LICENSE
Now, when I run:
php run.php
I get this error:
Fatal error: Class 'myns\childclass' not found in C:\wamp...\run.php on line 7
namespace myns;
in your child class? – Symphysis