- 產生 composer.json
<?php
namespace cwchiu\Lib;
class Hello {
private $name;
public function __construct($name = 'World') {
$this->name = $name;
}
public function hello() {
return 'Hello ' . $this->name;
}
}
"autoload":{
"psr-4" : {
"cwchiu\\Lib\\" : "src"
}
}
目錄結構變為
<?php
require 'vendor/autoload.php';
use cwchiu\Lib\Hello;
$a = new Hello('Arick');
echo $a->hello();
執行結果
- git tag -am "v0.0.1" 0.0.1
- git push origin 0.0.1
compsoer init --type=project
"repositories":[
{
"type":"vcs",
"url":"https://github.com/cwchiu/php-composer-lib"
}
],
"require": {
"cwchiu/php-composer-lib":"dev-master"
}
composer install
<?php
require 'vendor/autoload.php';
use cwchiu\Lib\Hello;
$a = new Hello('Arick');
echo $a->hello();
compsoer init --type=project
composer require cwchiu/php-composer-lib
<?php
require 'vendor/autoload.php';
use cwchiu\Lib\Hello;
$a = new Hello('Arick');
echo $a->hello();
- 如果無法正常引用套件, 可在 composer.json 加上 "minimum-stability": "dev",






