简体   繁体   中英

class not found autoloading psr-4

I am learning the Composer autoloading. I have some issues with PSR-4.

I am following this tutorial: https://vegibit.com/composer-autoloading-tutorial/

I have reviewed the code a few hours but I keep having the same issue when I load the script in the browser:

Fatal error: Uncaught Error: Class 'VegibitLibraryGreeting' not found in C:\\PHPWebPages\\aldemo\\aldemo4\\index.php:5 Stack trace: #0 {main} thrown in C:\\PHPWebPages\\aldemo\\aldemo4\\index.php on line 5

I have tried several solutions proposed in similar questions but I haven't solved it yet.

I attach some screenshots:

教程目录 在此处输入图片说明 composer.json和greeting.php

Please could someone help me finding out why is happening this error. Thank in advance.

Notes:

I am going to show the new code after the solution to allow anyone that will have the same issue can review the solution:

composer.json theme.php index.php

greeting.php:

You can't declare two namespaces in the same file. Namespace declaration should be in the very beginning of PHP file. You also should declare namespaces as they are presented in composer.json :

namespace Vegibit\Library;

Split this code to separate files.

index.php:

$vb = new Vegibit\Library\Greeting();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM