创建项目
- 创建一个项目test,目录结构为:

引入 Composer
使用 composer init 命令创建 composer.json 文件
**
$ composer initWelcome to the Composer config generatorThis command will guide you through creating your composer.json config.Package name (<vendor>/<name>) [asus/test]: king/testprojectDescription []: This is a test projectAuthor [yangyi <2497471389@qq.com>, n to skip]:Minimum Stability []:Package Type (e.g. library, project, metapackage, composer-plugin) []: projectLicense []: MITDefine your dependencies.Would you like to define your dependencies (require) interactively [yes]? nWould you like to define your dev dependencies (require-dev) interactively [yes]? n{"name": "king/testproject","description": "This is a test project","type": "project","license": "MIT","authors": [{"name": "yangyi","email": "2497471389@qq.com"}],"require": {}}Do you confirm generation [yes]? yes
在 composer.json 文件中加入 psr-4
**
{"name": "yangyi/laravel-teach","description": "This is a yangyi project","type": "library","license": "MIT","authors": [{"name": "yangyi","email": "2497471389@qq.com"}],"autoload":{"psr-4":{"yangyi\\app\\":"./src/"}},"require": {}}
注意:yangyi\app 代表命名空间,./src/ 代表对应的目录
运行composer更新命令,它会把我们需要的文件都自动下载到 vendor 目录下
composer update
现在的目录结构
在 src 下建立一个文件 ./src/database/Mysql.php
<?php
namespace yangyi\app\database;
class Mysql{
public function index(){
return 'This is a mysql';
}
}
在index.php 中 引入 ./vendor/autoload.php 就可以了
**
<?php
require './vendor/autoload.php';
use yangyi\app\database\Mysql;
$mysql = new Mysql();
echo $mysql->index();
测试输出
This is a mysql
