composer是什么呢?composer本意是:a person who writes music, especially as a professional occupation. 当然它不是用来艺术创作的。官网是这样介绍的:A Dependency Manager for PHP,简而言之就是:一个为PHP而生的依赖管理工具。
logo-composer-transparent5.png

相信绝大部分PHP开发者都使用过,那你有了解过它到底是怎么运行起来的吗?让我们一起来一探究竟。

composer项目初始化

  • composer.json
{
  "require": {
    "monolog/monolog": "2.0.2"
  },
  "autoload": {
    "psr-4": {
      "Acme\\": "src/"
    }
  }
}
  • composer install
$ composer install
  • index.php
require __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('app.log', Logger::WARNING));

// add records to the log
$log->warning('Foo');
$log->error('Bar');
  • 目录结构
.
├── composer.json
├── composer.lock
├── index.php
└── vendor

composer自动加载

通过一行require即可将整个vendor包加载进来,使用非常方便。

初始化过程

    1. vendor/composer
vendor/composer
├── ClassLoader.php
├── LICENSE
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── autoload_static.php
└── installed.json
    1. vendor/autoload.php
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit1074b760c98f6d9aec2b5c4768fab1ae::getLoader();
    1. vendor/composer/autoload_real.php
spl_autoload_register(array('ComposerAutoloaderInit1074b760c98f6d9aec2b5c4768fab1ae', 'loadClassLoader'), true, true);
public static function loadClassLoader($class)
{
    if ('Composer\Autoload\ClassLoader' === $class) {
        require __DIR__ . '/ClassLoader.php';
    }
}
    1. vendor/composer/ClassLoader.php
public function loadClass($class)
{
    if ($file = $this->findFile($class)) {
        includeFile($file);

        return true;
    }
}
/**
 * Scope isolated include.
 *
 * Prevents access to $this/self from included files.
 */
function includeFile($file)
{
    include $file;
}

演示示例

示例:魔术方法

function __autoload($name)
{
    echo $name;
    exit;
}

new aaa();

示例:spl_auto_register

spl_autoload_register(['myClass', 'run']);

class myClass
{
    public static function run($name)
    {
        echo $name;
        exit;
    }
}

$a = new abc();

示例:命名空间

  • 文件结构
.
├── Controller
│   └── UserController.php
├── Model
│   └── UserModel.php
└── index.php
  • UserController.php
namespace Controller;

use Model\UserModel;

class UserController
{
    public function index()
    {
        $obj = new UserModel();
        $db = $obj->index();
        echo "this is controller {$db}";
    }
}
  • UserModel.php
namespace Model;

class UserModel
{
    public function index()
    {
        return "this is model";
    }
}
  • index.php
define("APP_ROOT", __DIR__);

spl_autoload_register('loader');

function loader($cls) {
    $cls = str_replace("\\", "/", $cls);
    $path = APP_ROOT . "/{$cls}.php";
    if (file_exists($path)) {
        return require $path;
    }
    echo "{$cls} loader error~";
    exit;
}

$obj = new \Controller\UserController();
$obj->index();

结束语:当我们不仅熟练使用composer,也懂其原理,编码,又何尝不是在写诗呢?

WechatIMG35.jpeg


凌枫
1k 声望49 粉丝

PHP面试问答 [链接]