PHP 5.3之後,新增了Phar,可以將Library打包成phar檔(有點類似Java的jar檔),這樣可以讓專案更容易移植
打包phar(php.ini中的phar.readonly參數要改成Off)
//定義檔名 phar = new Phar('My.phar'); //預設直接執行phar檔會去跑index.php,假設Library中沒有index.php的話,可以使用setStub來設定 phar->setStub(''); //只定義目錄的話,預設會打包index.php phar->buildFromDirectory(dirname(__FILE__).'/class'); //打包class目錄底下的php檔案 phar->buildFromDirectory(dirname(__FILE__).'/class', '/\.php/'); //以 GZ 格式壓縮 phar->compressFiles( Phar::GZ ); //處理完成,寫入檔案 $phar->stopBuffering();
使用
require 'phar://My.phar/Test.php'; require 'phar://My.phar/Test2.php'; a = new Test(); a -> test_a(); b = new Test2(); b -> test_b();
Categories: PHP