{"id":66,"date":"2016-01-09T00:52:16","date_gmt":"2016-01-08T16:52:16","guid":{"rendered":"http:\/\/www.tra56.com\/?p=66"},"modified":"2016-01-09T00:52:16","modified_gmt":"2016-01-08T16:52:16","slug":"experienced-php-phar-10-minutes-tutorial","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/66.html","title":{"rendered":"PHP PHAR 10\u5206\u949f\u4f53\u9a8c\u6559\u7a0b"},"content":{"rendered":"

\n\t\u65e2\u7136\u662f10\u5206\u949f\u6559\u7a0b\uff0c\u5c31\u4e0d\u8981\u6307\u671b\u5f88\u8be6\u7ec6\u4e86\uff0c\u53ea\u662f\u4e3a\u4e86\u521d\u6b65\u4e86\u89e3\u4e0bPHAR\uff0c\u6211\u81ea\u5df1\u4e5f\u53ea\u662f\u521a\u63a5\u89e6\u800c\u5df2\n<\/p>\n

\n\tPHAR: \u5373 PHP Archive\uff0c\u5c06\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u5355\u4e2a\u6587\u4ef6\uff0c\u4ee5\u5229\u4e8e\u5206\u53d1\u548c\u5b89\u88c5\u7684\u673a\u5236\uff0c\u4f3c\u4e4e\u662f\u4eceJAVA\u7684JAR\u501f\u9274\u6765\u7684\u4e1c\u897f\u3002<\/span>\n<\/p>\n

\n\t<\/span>\n<\/p>\n

\n\t# \u76ee\u5f55\u7ed3\u6784
\nphar-demo\/ # \u8981\u6253\u5305\u7684\u5e94\u7528\u7a0b\u5e8f
\n  --Dates.php # \u91cc\u9762\u7684\u4e1c\u897f
\n<\/span>my-app.php # \u5728\u8fd9\u91cc\u53ef\u4ee5\u8c03\u7528phar\u6587\u4ef6
\nphar-builder.php # \u7528\u4e8e\u521b\u5efaphar\u6587\u4ef6\u7684\u6587\u4ef6
\nphpar-demo.phar # \u751f\u4ea7\u7684phar\u6587\u4ef6<\/span>\n<\/p>\n

\n\t\u9996\u5148\uff0c\u5efa\u7acbphar-demo\u6587\u4ef6\u5939\uff1a<\/span>\n<\/p>\n

\n\t
\n<\/span>\n<\/p>\n

\n\t <\/span>\n<\/p>\n

\/\/ phar-demo\/Dates.php\r\nnamespace PHX;\r\nclass Dates{\r\n    public static function next_week(){\r\n        $datetime = new \\DateTime();\r\n        $datetime->add(new \\DateInterval('P7D'));\r\n        return $datetime->format('Y-m-d');\r\n    }\r\n}<\/pre>\n

<\/p>\n

\n\t\n<\/p>\n

\n\t\u7136\u540e\uff1a<\/span><\/span>\n<\/p>\n

\n\t
\n<\/span>\n<\/p>\n

\n\t <\/span>\n<\/p>\n

\/\/ phar-builder.php\r\n$phar = new Phar('phar-demo.phar');\r\n$phar->buildFromDirectory(dirname(__FILE__).'\/phar-demo', '\/\\.php$\/');\r\n$phar->compressFiles( Phar::GZ );\r\n$phar->stopBuffering();\r\n$phar->setStub( $phar->createDefaultStub('Dates.php') );<\/pre>\n

\n\t\u7136\u540e\u8fd0\u884c\u4e00\u4e0b phar-builder.php\uff0c\u8fd9\u6837phar-demo.phar\u5c31\u751f\u6210\u51fa\u6765\u4e86\uff0c\u63a5\u7740\uff1a<\/span><\/span>\n<\/p>\n

\n\t
\n<\/span>\n<\/p>\n

\n\t<\/p>\n

\/\/ my-app.php\r\n\/\/ # \u7528\u547d\u4ee4\u884c\u8fd0\u884c\uff1a php my-app.php\r\nrequire_once 'phar:\/\/phar-demo.phar';\r\necho 'Next week is '. PHX\\Dates::next_week() . PHP_EOL;<\/pre>\n

\n<\/span>\n<\/p>\n

\n\t<\/p>\n

\n\t\t\u5e0c\u671b\u501f\u6b64\u629b\u7816\u5f15\u7389\uff0c\u671f\u5f85\u5927\u725b\u4eec\u7684\u6559\u7a0b\uff0c\u66f4\u597d\u5730\u5b66\u4e60PHAR\uff01\n\t<\/p>\n

\n\t\t \n\t<\/p>\n

<\/span>\n<\/p>\n

<\/p>\n

\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"

\u65e2\u7136\u662f10\u5206\u949f\u6559\u7a0b\uff0c\u5c31\u4e0d\u8981\u6307\u671b\u5f88\u8be6\u7ec6\u4e86\uff0c\u53ea\u662f\u4e3a\u4e86\u521d\u6b65\u4e86\u89e3\u4e0bPHAR\uff0c\u6211\u81ea\u5df1\u4e5f\u53ea\u662f\u521a\u63a5\u89e6\u800c\u5df2 PHAR: \u5373 P […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/66"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/66\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}