php静态变量与方法与phar的使用
本节用类与静态变量改造之前的例子:php根据命令行参数生成配置文件
ghostinit.php:
<?php
class ghostinit{
static $version = 'ghost version is 1.1';
static $projName = '';
static $author = 'ghostwu';
static function init(){
echo "pls input project name?" . PHP_EOL;
self::$projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
self::$author = fgets( STDIN );
echo "您输入的项目信息如下:" . PHP_EOL;
echo self::$projName . PHP_EOL;
echo self::$author . PHP_EOL;
}
static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
}
?>
ghost:
#!/usr/bin/php
<?php
require "ghostinit.php"; $result = ''; if( $argc >= 2 ) {
$argv[1] == '-v' && $result = ghostinit::$version;
$argv[1] == 'make' && ghostinit::make();
$argv[1] == 'init' && ghostinit::init();
} echo $result . PHP_EOL;
执行结果:
ghostwu@dev:~/php/php1/3$ ls
done ghost ghostinit.php
ghostwu@dev:~/php/php1/3$ ./ghost init
pls input project name?
test
pls input author?
ghostwu
您输入的项目信息如下:
test ghostwu ghostwu@dev:~/php/php1/3$ ls
done ghost ghostinit.php
ghostwu@dev:~/php/php1/3$ ./ghost make ghostwu@dev:~/php/php1/3$ ls
done ghost ghostinit.php ghost.phar
ghostwu@dev:~/php/php1/3$ ./ghost -v
ghost version is 1.1
ghostwu@dev:~/php/php1/3$
callstatic继续改造:
ghostinit.php:
<?php
class ghostinit{
static $v = 'ghost version is 1.1';
static $projName = '';
static $author = 'ghostwu';
static function init(){
echo "pls input project name?" . PHP_EOL;
self::$projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
self::$author = fgets( STDIN );
echo "您输入的项目信息如下:" . PHP_EOL;
echo self::$projName . PHP_EOL;
echo self::$author . PHP_EOL;
}
static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
static function __callstatic( $m, $args ){
echo 'error function';
}
}
?>
ghost:
#!/usr/bin/php
<?php
require "ghostinit.php"; $result = ''; if( $argc >= 2 ) {
$p = $argv[1];
if( substr( $p, 0, 1 ) == '-' ) {
$p = substr( $p, 1 );
$result = isset( ghostinit::$$p ) ? ghostinit::$$p : 'error';
}else {
$result = ghostinit::$p();
}
} echo $result . PHP_EOL;
把配置独立成一个类
ghostconfig.php: 把这两个属性注释,也可以正常运行, php允许动态增加成员变量(类的属性)
<?php
class ghostconfig{
public $projName;
public $author; }
ghostinit.php
<?php
require( "ghostconfig.php" ); class ghostinit{
static $v = 'ghost version is 1.1'; static function init(){
$conf = new ghostconfig();
echo "pls input project name?" . PHP_EOL;
$conf->projName = fgets( STDIN ); echo "pls input author?" . PHP_EOL;
$conf->author = fgets( STDIN ); echo "您输入的项目信息如下:" . PHP_EOL; echo json_encode( $conf );
} static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
} static function __callstatic( $m, $args ){
echo 'error function';
} } ?>
利用顶级类stdClass代替config类,这样就减少了一个类,这个config类目前只用到了一次,完全可以用stdClass再次简化
<?php
class ghostinit{
static $v = 'ghost version is 1.1';
static function init(){
$conf = new stdClass();
echo "pls input project name?" . PHP_EOL;
$conf->projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
$conf->author = fgets( STDIN );
echo "您输入的项目信息如下:" . PHP_EOL;
echo json_encode( $conf );
}
static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
static function __callstatic( $m, $args ){
echo 'error function';
}
}
?>
生成配置信息,再次简化,变成公共模块:
static function init(){
echo "pls input project name?" . PHP_EOL;
$projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
$author = fgets( STDIN );
echo "您输入的项目信息如下:" . PHP_EOL;
echo json_encode( self::getConfig( [ 'proj_name' => $projName, 'author' => $author ] ) );
}
static function getConfig( $conf ){
$std = new stdClass();
foreach( $conf as $k => $v ){
$std->$k = $v;
}
return $std;
}
php静态变量与方法与phar的使用的更多相关文章
- Objective-C 静态变量 使用方法
详解Objective-C中静态变量使用方法 Objective-C中静态变量使用方法是本文要介绍的内容,Objective-C 支持全局变量,主要有两种实现方式:第一种和C/C++中的一样,使用&q ...
- Qt中静态变量使用方法
静态变量可以在各个页面之前使用 先定义一个用于存放静态变量的类 例如datavar 在datavar.h中添加如下代码 #ifndef DATAVAR_H #define DATAVAR_H #inc ...
- js 写成类的形式 js 静态变量 js方法 属性 json类
function ClassStudentList() { //[{"Cid":"0d","Students":[{"Sid&qu ...
- PHP引用操作以及外部操作函数的局部静态变量的方法
通过引用方式在外部操作函数或成员方法内部的静态变量 下面举个简单的例子,说明三个关于引用方面的问题: 1. 参数引用后函数内进行类型转换同样是地址操作 2. 参数引用后再传递给其他函数时需要再次添加引 ...
- Spring注入静态变量的方法,以及CXF如何获取客户端IP
1.如果使用@Resource注解来注入静态变量的,服务器启动就会报错的.可以新增一个set方法,同时在set方法上用@Resource注解来注入. 2.或者直接在Spring的配置文件中使用< ...
- JVM存储位置分配——java中局部变量、实例变量和静态变量在方法区、栈内存、堆内存中的分配
Java中的变量根据不同的标准可以分为两类,以其引用的数据类型的不同来划分可分为“原始数据类型变量和引用数据类型变量”,以其作用范围的不同来区分可分为“局部变量,实例变量和静态变量”. 根据“Java ...
- Java中局部变量、实例变量和静态变量在方法区、栈内存、堆内存中的分配
转自:https://blog.csdn.net/leunging/article/details/80599282 感谢CSDN博主「leunging」的总结分享 ———————————————— ...
- Java中读取配置文件中的内容,并将其赋值给静态变量的方法
应用场景 项目开发中某个功能需要抽取成方法写成一个工具类,提供给别人使用.写过工具类的人都知道,工具类中的方法一般都是静态方法,可以直接使用类名点方法名调用, 使用很方便,比如判断某个对象是否为空的方 ...
- FreeMarker调用Java静态方法以及静态变量的方法
这里介绍下在类似 Spring+FreeMarker 的架构中如何在FreeMarker中访问Java中的静态方法以及静态变量. 一.首先为了方便以及可复用我们创建一个工具类 /** * FreeMa ...
随机推荐
- OCP考试062题库出现大量新题-18
choose two Examine this command executed on a client that is remote from the database server. SQL> ...
- Linux系统文件压缩与备份(5)
在 Linux 系统选有相当多的压缩命令可以使用,这些压缩指令可以让我们更方便的从网上下载大型文件,本章第一节内容我们就来谈谈这个 Linux 系统下常用的几种压缩格式吧. 谈完了压缩后,我们接着来说 ...
- BZOJ 1012--[JSOI2008]最大数maxnumber(二分&单调栈)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 14142 Solved: 6049[Subm ...
- 表单控件 css的三中引入方式css选择器
1. 表单控件: 单选框 如果两个单选的name值一样,会产生互斥效果 <p> <!--单选框--> 男<input type="radio" nam ...
- Spring Boot中使用@Async实现异步调用
在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsyn ...
- idea导入myeclipes项目、运行项目
1. 导入,部署: https://blog.csdn.net/u010570551/article/details/51510447 2. idea导入MyEclipse Web项目时,服务器搭建运 ...
- 课程三(Structuring Machine Learning Projects),第一周(ML strategy(1)) —— 1.Machine learning Flight simulator:Bird recognition in the city of Peacetopia (case study)
[]To help you practice strategies for machine learning, the following exercise will present an in-de ...
- 阿里巴巴Java开发规范---个人总结
一.编程规约 (一) 命名规约 1. [强制]所有编程相关命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ / ...
- JSPatch动态更新APP
JSPatch,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug. 用途 是否有过这样 ...
- github绑定自己的域名
1.进入需要绑定域名的项目里面,然后新建一个文件CNAME,没有后缀的. 2.在文件里面输入你的域名,然后保存.github这边就完成了. 3.然后去你购买域名的网站,进入控制台,找到域名,然后域名解 ...