PHP laravel class not found. How can I use namespaces to fix this?


I'm using the Laravel framework and have a directory of the following structure:



In mymodels/Statistic class, I'm trying to access apresent method which calls a protected property which then references my presenter:

protected $presenter = 'presenters\StatisticPresenter';

public function present() {
    return new $this->presenter($this); 

Yet, this returns the error:Class 'presenters\StatisticPresenter' not found. I realize this is a namespacing error of some sort, and I've tried watching a few videos on how it works, but I simply can't wrap my head around it. I have already added my presenter folder to mycomposer.json file. For example, adding this to the top of myStatistic model does not work:

use presenters\StatisticPresenter;

How can I fix this?




Do the followings;

  • Mark your namespace in StatisticPresenter.php ? (at the top of file "namespace presenters;")
  • Add PSR-4 class map to your composer

{ "autoload": { "psr-4": { "presenters\\": "app/presenters/" } } }

  • run "composer dump-autoload" once and you wont need to run this command again for the "presenters" namespace if you add new classes into "app/presenters/ folder"
  • Test your class with "use presenters/StatisticPresenter;"
  • If you can access your class you dont need to change your code your present() function will be valid

People are also looking for solutions to the problem: PHP OOP NOT ADDING


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.