PHP Beautifier是一个用php编写的php代码美化工具。通过pear可以很方便的安装并使用。
简单介绍下载windows下安装php beautifier和使用它格式化代码的方法:
1.确保php5环境下已经安装了pear,如果没有安装请参考文章《windows下安装pear包管理器》。
2.pear下安装php beautifier:
在上网环境下,CMD命令行下运行 pear install php_beautifier-0.1.15,自动下载包并安装。
3.php beautifier的使用:
安装好后,可以在CMD命令行下运行 php_beautifier -h 查看使用说明。
命令参数项:
--input or -f <file> 带格式化代码文件目录或文件名,默认为标准输入流
--output or -o <out> 格式化代码输出目录或文件,默认标准输出流
--indent_tabs or -t <int> 缩进使用tab
--indent_spaces or -s <int> 缩进使用空格,默认也使用空格缩进的。
--filters or -l <fil_def> 增加过滤
--directory_filters or -d <path> 包含的目录或文件
--compress or -c <type> 格式化后输出目录或文件打包
--recursive or -r 对目录下子目录递归扫面执行。
--version 显示版本
--help or -? 显示帮助说明
接下来,我们就可以运行命令来格式化我们凌乱的代码了。
例如有一个比较凌乱的foo.php文件:
<?php error_reporting(0);function shutdown_handler(){if( $error=error_get_last() ) {
print_r( $error );} }
register_shutdown_function( 'shutdown_handler' );
class foo{
public $bar='hello world!';
public function __construct(){} }
echo var_export( new foo(), true );
?>
使用php_beautifier,执行命令,php_beautifier -t <待格式化文件路径>\foo.php <格式化文件路径>\foo.beautiful.php
执行过程中,可能会报“Notice: Use of undefined constant T_NAMESPACE - assumed 'T_NAMESPACE' in E:\php5\PEAR\PHP\Beautifier.php on line 377”。
这是因为T_NAMESPACE这个常量是在php5.3种才有定义的,如果非5.3版本则会出现这个Notice,可以忽略,不影响代码格式化。
这个时候我们就可以看到foo.php经过格式化后的代码文件foo.beautiful.php:
<?php
error_reporting(0);
function shutdown_handler() {
if ($error = error_get_last()) {
print_r($error);
}
}
register_shutdown_function('shutdown_handler');
class foo {
public $bar = 'hello world!';
public function __construct() {
}
}
echo var_export(new foo(), true);
?>