Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer Gives Error, "Class Not Found"

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

like image 959
Omar Tariq Avatar asked Oct 22 '15 19:10

Omar Tariq


People also ask

How do you solve a composer error?

To solve the Memory exhausted issue, you can try running composer with an Unlimited memory flag like this: php -d memory_limit=-1 /usr/local/bin/composer [COMMAND] or php -d memory_limit=-1 composer. phar [COMMAND] when using a local composer. If that doesn't help, you can upgrade your hosting plan.

Why is my composer not working?

Try clearing Composer's cache by running composer clear-cache . Ensure you're installing vendors straight from your composer. json via rm -rf vendor && composer update -v when troubleshooting, excluding any possible interferences with existing vendor installations or composer. lock entries.


1 Answers

In composer.json you defined that for src folder you use myns namespace, so in your childclass.php you should use

namespace myns;

It's also unnecessary to add:

require_once 'parentclass.php';

or

use myns\parentclass as parentclass;

so your childclass.php should look like this:

<?php

namespace myns;

class childclass extends parentclass
{
    public function abc()
    {
        echo 'hello world';
    }
}

In addition in run.php file you might replace:

use myns\childclass as childclass;

into

use myns\childclass;

You don't need to use as if you don't want to use other name for class.

You should also consider using namespaces with capital letter (Studly caps) and the same for classes. Instead of myns use MyNs, instead of parentclass use ParentClass. You should look at PSR-1 coding standard and PSR-2 coding standard to follow best coding practises.

like image 88
Marcin Nabiałek Avatar answered Sep 30 '22 10:09

Marcin Nabiałek