duangekui7451 2016-09-27 07:36 采纳率: 0%
浏览 34
已采纳

试图通过控制台应用程序了解PSR-4自动加载。 我究竟做错了什么?

I'm trying to understand PSR-4 autoloading by writing a very basic example app that runs on the console.

Here is my app's (directory) structure:

app/ -library/auth/Author/AuthorInterface.php -library/auth/Author/Author.php -library/auth/Authorize/ -library/database/ -vendor/composer/ -vendor/autoload.php -composer.json -composer.lock -composer.phar -manager.php

All I'm trying to do is run manager.php and have it echo a return statement from Author.php

AuthorInterface.php:

namespace Vee\Auth\Author;

interface AuthorInterface
{
    public function write();
}

Author.php

namespace Vee\Auth\Author;

class Author implements AuthorInterface
{
    function __construct()
    {
        return "Hello";
    }

    public function write()
    {
        return "Hello.write";
    }
}

manager.php:

require "vendor/autoload.php";

use Vee\Auth\Author;

echo new Author();

composer.json:

{
    "autoload": {
        "psr-4": {
            "Vee\\": "library/"
        }
    },
    "require": {
        "monolog/monolog": "^1.21"
    }
}

And here's the error that I see when I try to run manager.php:

$ php manager.php 

Fatal error: Class 'Vee\Auth\Author' not found in /<path-to-app>/manager.php on line 7

I have already run:

$ php composer.phar update

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

What am I missing?

EDIT:

I'm running PHP 5.5 on a Mac, if that makes any difference-

$ php --version
PHP 5.5.36 (cli) (built: Jun 12 2016 23:47:46) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
  • 写回答

1条回答 默认 最新

  • dongtang6681 2016-09-27 07:42
    关注

    By psr-4 2.6

    All class names MUST be referenced in a case-sensitive fashion.

    and 3.3

    The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

    Try renaming auth to Auth.

    Update:

    Just noticed that your use statement in manager.php is wrong. Try:

    require "vendor/autoload.php";
    
    use Vee\Auth\Author\Author;
    
    print (new Author())->write();
    

    In addition you can't echo a class, even when you are returning string from constructor.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)