Damian W
Damian W

Reputation: 568

Trouble with Composer Autoloading: not loading all files

I've been building a web app of my own for a while now, but have completely hit against the wall with a fatal error in PHP.

Short story: I'm working on the login / user section of the project. login.php handles the login stuff, while student.php handles the account stuff.

When trying to complete doLogin, I achieve the fatal error that states student::getProfile() is an "undefined method". student::getProfile() is called as part of validate() inside the login class.

Any assistance would be greatly appreciated! Thanks :)

EDIT: With the help of @deceze I've been able to narrow the issue down to the fact that Composer isn't autoloading all of my classes; only some. Would anyone be able to assist?

EDIT 2: I checked autoload_classmap.php which was generated by Composer, and all my core classes and models are listed! If they're listed in the classmap, why isn't Composer loaded them?


project directory

application/
    config/
    controller/
    core/
        (core items such as auth, app, view rendering + view controller)
    model/
        (speciality functions such as login, registration + user)
    view/
public/
    index.php
    .htaccess
vendor/
    autoload.php
composer.json
.htaccess

note: /public/index.php calls `require '../vendor/autoload.php';`

composer.json

"autoload": {
    "psr-4": {
        "": [
            "application/core/",
            "application/model/"
        ]
    }
}

Upvotes: 4

Views: 653

Answers (1)

Damian W
Damian W

Reputation: 568

After much tedious searching, I've found the error in my ways - and it's pretty silly!

I have a controller named student while also having a model named student, and as such I was inadvertently trying to call a class that's technically already being called. As such, it was looking in the first student class for the function, rather than the other which actually contained that specific function.

Composer doesn't realise if there is already a class with that name pre-declared, and just negates it without any entry in the PHP error log.

To avoid this error - and any further class name mixups in the future - I decided to use individual namespaces for both core classes and model classes. This separates those classes into 'sub-sections' as such, allowing the use of classes named the same (albeit in different namespaces).


PHP Manual: Namespaces

Sitepoint: How to Use PHP Namespaces, Part 1: The Basics

Upvotes: 1

Related Questions