Why am I getting error even after declaring namespaces correctly?

怎甘沉沦 提交于 2019-12-08 09:50:28

问题


This is my folder structure

This is my composer.json code

{
"name": "hashstar/hashstar",
"description": "Online shopping",
"type": "project",
"license": "private",
"authors": [
    {
        "name": "Akshay Shrivastav",
        "email": "akshayshrivastav866@gmail.com"
    }
],
"minimum-stability": "dev",
"require": {
    "php": ">=5.5.0"
},
"autoload": {
    "psr-4": {
        "modules\\": "/"
    }
  }
}

With this code, i have successfully generated the autoload.php thing in the vendor folder.

I have a file name HelloWorld.php in modules folder

In the very root directory as the snapshot shows below i have an file named index.php the code is as follows:

<?php
    require_once __DIR__ . '/vendor/autoload.php';
    use modules\HelloWorld\Greetings;
    echo Greetings::sayHelloWorld();
?>

After running this codes I am getting the errors like this

I don't know what the error is I have been searching the solution on google & doing various things from last few hours. Any helps would be appreciated.


回答1:


Actually, i got the error I wasn't doing two things.

1.) Using vendor name folder structure with namespaces in it.
2.) I wasn't using dump-autoload thing. After doing these two things finally my code is working. Thanks for the help :)


来源:https://stackoverflow.com/questions/42179503/why-am-i-getting-error-even-after-declaring-namespaces-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!