composer.json自动加载PHP类

时间:2018-12-28 15:41:22

标签: composer-php

我对composer.json中的自动加载感到很困惑:

我的项目结构如下:

/
  app.php
  composer.json
  src/
    Human.php

Human.php像这样:

<?php 
namespace Society;

class Human {
    var $name;

    function __construct() {
        print("Constructing human");
    }
    function getName() {
        return $this->$name;
    }
}

app.php就像:

<?php
use Society\Human;

$h1=new Society\Human();

$h1->getName();

最后我的composer.json就像:

{
    "name": "tr/society-emulation",
    "require": {
        "php": ">=5.5.0",
        "monolog/monolog": "^1.17"
    },
    "require-dev": {
        "phpunit/phpunit": ">=4.8 < 6.0"
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "config": {
        "process-timeout" : 0
    },
    "autoload": {
        "psr-4": {
            "Society\\": "src/"
        }
    }

}      

当我运行app.php时,错误提示为:Class 'Society\Human' not found

有人可以提示如何使事情正常吗?

0 个答案:

没有答案