Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kaizenCoder's avatar

Why doesn't my class autoload?

I've created a custom class OrderRepository with the following:

Custom Class

# path: app/Acme/Repositories/OrderRepository.php

<?php namespace App\Acme\Repositories;

class OrderRespository{
    public function __contstruct()
    {
        # code...
    }
}

Controller

# Snippets of what I've added

use App\Acme\Repositories\OrderRespository;

    private $order;

    public function __contstruct(OrderRespository $order){
        $this->order = $order;
    }

    public function index()
    {
        dd($this->order);
    }

I get a null object when hitting the uri.

When I tried dd(new OrderRepository());, I get the following error:

Class 'App\Acme\Repositories\OrderRespository' not found

What am I missing here?

p.s: Using laravel 5.1

0 likes
9 replies
bashy's avatar

And you've done a composer dump-autoload for the project?

jimmck's avatar

Where are your creating your OrderRepository instance? Normally you don't have to dump autoload when creating a new class in the App hierarchy. Need to see your more code. Post your autoload section in your composer.json file.

kaizenCoder's avatar

@bashy : Yup did that.

@jimmck :

Where are your creating your OrderRepository instance?

In the index method.

composer file

 "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {"App\\": "app/"}
    },
zachleigh's avatar

In order to inject a dependency like that, you need to make App aware of it by registering the class in a service provider. Normally when using repositories, you would bind an interface to the concrete class and then inject the interface rather than the concrete class. Otherwise you're simply exchanging a hard dependency on a model with a hard dependency on another class.
About the service container : https://laravel.com/docs/5.2/container

ganeshghalame's avatar

Add below code in your "autoload" section of composer.json

"files": [
              " app\Acme\Repositories\OrderRespository"
        ],

Then run below command :

composer dump-autoload 

that's it you can use it anywhere by adding use statement.

michaeldyrynda's avatar
Level 41

You have a typo in your class name.

class OrderRespository

Your file name is app/Acme/Repositories/OrderRepository.php.

Either rename the file to match the class, or fix the class declaration so it matches the file name. The latter is probably the better option.

1 like
jimmck's avatar

If your code is in app you are being autoloaded. Did you fix your typo? You don't need further composer entries.

kaizenCoder's avatar

Yup it was the typos. I have to stop coding into the dark!

thanks all!

1 like

Please or to participate in this conversation.