composer autoload自动加载

PSR-4

composer.json
{
    "name": "huyongjian/php",
    "autoload": {
        "psr-4": {
            "Huyongjian\\": "Huyongjian/",
            "Huyongjian\\Controller\\": "Huyongjian/Controller/",
            "Huyongjian\\Model\\": "Huyongjian/Model/"
        }
    },
    "authors": [
        {
            "name": "huyongjian",
            "email": "308830232@qq.com"
        }
    ],
    "require": {}
}

解释: Huyongjian命名空间映射到 /Huyongjian目录 Huyongjian\Controller命名空间映射到 /Huyongjian/Controller目录 Huyongjian\Model命名空间映射到 /Huyongjian/Model目录

composer更新

对应生成的配置文件:vendor/composer/autoload_psr4.php。

composer update

files

composer.json
{
    "name": "huyongjian/php",
    "autoload": {
        "files": ["functions/functions.php"]
    },
    "authors": [
        {
            "name": "huyongjian",
            "email": "308830232@qq.com"
        }
    ],
    "require": {}
}
/functions/functions.php
<?php
//加法
function add($num1, $num2){
    return $num1 + $num2;
}
//乘法
function mul($num1,$num2){
    return $num1 * $num2;
}
/index.php
<?php
require "vendor/autoload.php";


$num1 = 5;
$num2 = 6;
$result = add($num1,$num2);
echo $result;
echo "<br>";

$result = mul($num1,$num2);
echo $result;
composer更新
composer dump-autoload
浏览器显示
11
30

composer autoload自动加载

上一篇:Hystrix简单使用


下一篇:N1BOOK afr_1 1 ---BUUCTF