requireonce php PHP使用Phar打包控制台程序

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>requireonce php PHP使用Phar打包控制台程序

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>1. 介绍

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>1.1 介绍

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>php脚本有着非常强大的库支持requireonce php,可以轻松做出特别强大的程序。php不仅仅可以搭建各种各样的网站系统、平台系统,还可以开发基于控制台运行的程序。不过使用php开发的控制台程序在使用的时候需要保证包含库文件的相对路径结构,这个在使用上不是很方便。如果有一个方法可以把整个php项目都变成一个php文件就好了。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>其实php自身支持一种叫做phar的打包方式requireonce phprequireonce php PHP使用Phar打包控制台程序,可以使用自带的Phar对象将我们项目里的所有文件都打包到一个文件里面,这就很好地解决了我们的需求了。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>2. 编码转换器

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>我们先使用php开发一个编码转换器,支持字符串的MD5哈希加密、BASE的编码/解码、URL的编码/解码的功能。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>只看需求大家都知道了,这些功能都是php内建的函数可以实现的功能requireonce php,我们需要做的就是把它们封装一下而已。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>2.1 lib/.inc.php

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>编码对象,用友实现需求的各种编码、解码的功能。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”><pre class="syl-page-code" style="background: #e4daf5; color: #4a1c90; padding: 1.2em; border-radius: 6px; overflow-x: auto; margin: 1.6em 0; font-family: Consolas, Monaco, monospace; font-size: 14px; line-height: 1.6;”>namespace TFPHPDemo;
class TFEncoder{
    private string $CRLF;
    public function __construct(){
        $this->CRLF = "n";
    }
    public function md5(string $org):string {
        return md5($org);
    }
    public function base64(string $org, bool $decode=false):string {
        if($decode){
            return base64_decode($org);
        }
        return base64_encode($org);
    }
    public function url(string $org, bool $decode=false):string {
        if($decode){
            return urldecode($org);
        }
        return urlencode($org);
    }
    public function printLine($org){
        echo $org. $this->CRLF;
    }
    public function dumpLine($org){
        var_dump($org);
    }
}

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>2.2 .php

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.baidu.com/it/u=218651679,5633893&fm=253&fmt=JPEG?w=435&h=278″ alt=”requireonce php”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #b280e0;”>

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>入口程序,通过调用对象的方法实现需求里面的那些编码、解码功能。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”><pre class="syl-page-code" style="background: #e4daf5; color: #4a1c90; padding: 1.2em; border-radius: 6px; overflow-x: auto; margin: 1.6em 0; font-family: Consolas, Monaco, monospace; font-size: 14px; line-height: 1.6;”>use TFPHPDemoTFEncoder;
include_once ('lib/TFEncoder.inc.php');
$method = $_SERVER['argv'][1];
$origin = $_SERVER['argv'][2];
$myEncoder = new TFEncoder();
switch($method){
    case "md5":
        $myEncoder->printLine($myEncoder->md5($origin));
        break;
    case "b64e":
    case "base64encode":
        $myEncoder->printLine($myEncoder->base64($origin));
        break;
    case "b64d":
    case "base64decode":
        $myEncoder->printLine($myEncoder->base64($origin, true));
        break;
    case "ue":
    case "urlencode":
        $myEncoder->printLine($myEncoder->url($origin));
        break;
    case "ud":
    case "urldecode":
        $myEncoder->printLine($myEncoder->url($origin, true));
        break;
    case "-V":
    case "--version":
        print "Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
";
        break;
    default:
        print "Usage: TFEncoder [method] [origin]
method:
  md5                   - MD5
  b64e, base64encode    - BASE64 encode
  b64d, base64decode    - BASE64 decode
  ue, urlencode         - URL encode
  ud, urldecode         - URL decode
For more information, please see:
<http://tongfu.net/demo/tfencoder>.
";
        break;
}

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.toutiaoimg.com/tos-cn-i-axegupay5k/3b5ddab29e5040e6a39530a983fa4b82~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1791586879&x-signature=B6vBnqWPBXeKpB2QSJzjNSf1DqU%3D” alt=”requireonce php”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #b280e0;”>

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>3. 开启Phar打包

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>默认Phar打包功能的关闭的,我们需要到php.ini里面打开它。找到php.ini里面的Phar这一块requireonce php PHP使用Phar打包控制台程序,将phar.改成Off即可。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.toutiaoimg.com/pgc-image/984331d56b484682a2c8a15d5a037060~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1791586879&x-signature=pDuu9yAFusShlKvdEt%2BlUs3TKsQ%3D” alt=”requireonce php”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #b280e0;”>

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>4. 使用Phar打包

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>现在我们使用Phar对象把项目打包一下。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>4.1 .php

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”><pre class="syl-page-code" style="background: #e4daf5; color: #4a1c90; padding: 1.2em; border-radius: 6px; overflow-x: auto; margin: 1.6em 0; font-family: Consolas, Monaco, monospace; font-size: 14px; line-height: 1.6;”>$currDir = __DIR__. "/";
$physicalDirRoot = __DIR__. "/";
$pharSaveFilePath = $physicalDirRoot. "TFEncoder.phar";
$pharEntryFilePath = "TFEncoder.php";
$myPhar = new Phar($pharSaveFilePath);
$myPhar->addEmptyDir("lib/");
$myPhar->addFile(
    $physicalDirRoot. "lib/TFEncoder.inc.php",
    "lib/TFEncoder.inc.php"
    );
$myPhar->addFile(
    $physicalDirRoot. "TFEncoder.php",
    "TFEncoder.php"
);
$myPhar->compressFiles(Phar::GZ);
$myPhar->stopBuffering();
$myPhar->setStub($myPhar->createDefaultStub($pharEntryFilePath));

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>4.2 讲解

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>4.3 打包

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.baidu.com/it/u=339774539,3800782001&fm=253&fmt=JPEG&app=138&f=JPEG?w=772&h=500″ alt=”requireonce php”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #b280e0;”>

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>现在可以使用.php打包项目了。

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.toutiaoimg.com/pgc-image/4bea82f89c404bafaa1c36c3b7e9c8a2~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1791586879&x-signature=unhWyo9BYOr4TpbdxGRkOzY%2BApI%3D” alt=”requireonce php”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #b280e0;”>

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>5. 总结

<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>可以看到我们使用php的phar打包工具可以制作一些常用的命令行程序,这些程序使用phar打包之后可以很方便的进行分享和部署了。

公告:
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299

资源库 - 资源分享下载网 » requireonce php PHP使用Phar打包控制台程序

常见问题FAQ

关于资源售价和售后服务的说明?
本站所有资源的标价均为本站收集资源的辛苦费,不代表资源本身的价值。软件是高智慧高价值的商品,不可能是白菜价。本站资源标价只是赞助费用,收取的赞助费仅用来维持本站的日常运营!毕竟收集整理资料需要投入云计算资源和站长大量的精力。
代码有没有售后服务和技术支持?
由于代码的运行具有不可预见性,本站不保证代码完整可运行,不提供技术支持和售后服务。 本站原创代码都是站长自己开发的,可以有偿提供技术支持服务。 网站里标明【亲测】的代码都是站长亲测过的,其他的代码由于精力有限,没有一一测试,不能保证代码就一定能够使用,更没有技术支持服务,下载前请自行斟酌。
有没有搭建服务?
由于搭建服务比较费时费力,所以本站除了原创代码外均不提供搭建服务。本站分享代码纯属兴趣爱好,不以盈利为目的,请勿咨询有没有搭建服务,谢谢理解。
链接地址失效了怎么办?
请带上资源链接地址联系客服,工作时间内我们看到后将第一时间回复。
关于解压密码
本站资源一般都没有加密,如果发现需要解压密码的,那么就输入 hao.35dc.com 试试。

发表评论

资源库,由老程序员细心甄别、精心筛选,只为提供优质的源码资源

关于我们 联系我们