8

PHP特性小结 - Limboy's HQ

 3 years ago
source link: https://limboy.me/2010/08/27/hidden-php-features/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP特性小结

2010-08-27

其实就是总结了一下这个帖子

PHP文档的特点:

  • 用户评论是一大亮点
  • 要查询某个函数,输入:www.php.net/function 就会跳转到该函数的具体页

PHP的数组是我用过的语言中最强大的,其实就是个有序映射表(ordered map),并为各种使用作了优化。可以看作列表、哈希表、字典、集合、栈、队列。

可以扩展文件流,比如excel流处理,可以通过下面的语句来创建一个excel文件

<?php
$fp = fopen("xlsfile://tmp/test.xls", "wb");
if (!is_resource($fp)) { 
    die("Cannot open excel file");
}

$data= array(
    array("Name" => "Bob Loblaw", "Age" => 50),  
    array("Name" => "Popo Jijo", "Age" => 75),  
    array("Name" => "Tiny Tim", "Age" => 90)
); 

fwrite($fp, serialize($data));
fclose($fp);
<?php
	public function __get($key) { ... }
	public function __set($key, $value) { ... }
	public function __call($func, $args) { ... }
	public function __toString() { ... }
	//...

or/and

<?php
isset($foo) OR print '$foo is not defined';

spl_autoload_register

使用spl_autoload_register,而不是__autoload,因为spl_autoload_register允许设置多个实例,而不会覆盖。

<?php
spl_autoload_register('autoloader');
function autoloader($class)
{
	//这里可以对$class进行处理
}

set_include_path

设置引用路径,比如引用zend的libraries时。

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../libs/');`

处理远程文件/url

<?php
$fp = fopen('http://example.com');
$str = file_get_contents('http://example.com/file');
$imageInfo = getimagesize('ftp://user:[email protected]/image/name.jpg');

虽然这些函数的健壮性和稳定性不如curl,但毕竟使用方便。

strtr

使用方便,效率高,用来替换字符串内的特定值。有点类似str_replace,但更灵活。

<?php
echo strtr('hello world', array('world' => 'hell'));

extract

顾名思义,释放一个数组内的元素,构造模板引擎的时候尤为有用

<?php
$arr = array('foo' => 'bar');
extract($arr);
echo $foo; // output 'bar'

func_get_args

获取传递过来的参数

<?php
function test() {
    $args = func_get_args();
    echo $args[2]; // will print 'd'
    echo $args[1]; // will print 3
}

test(1,3,'d',4);

command line interface,可以在命令行里运行php脚本,用来处理日常任务也非常方便。对了别忘了一个非常有用的参数 -a,进入交互模式

<?php
php -a

Interactive shell
php > 

载入的文件可以有返回值

这个比.ini文件更加灵活,你可以在文件里运行一段程序后,返回运行结果

<?php
// config.php
return array(
    'db' => array(
	'host' => $_SERVER['HTTP_HOST'] == '127.0.0.1'?'localhost':'example.org',
        'user' => 'usr',
        // ...
    ),
    // ...
);

// index.php
$config = include 'config.php';
echo $config['db']['host']; // example.org

在函数内定义静态变量

把类的特性移植到函数里了,比如有一些运算量较大的函数,可以把运算结果放到这个静态变量,下次调用时检查一下该变量是否已经被赋值。

<?php
function foo($arg1)
{
    static $cache;

    if( !isset($cache[md5($arg1)]) )
    {
        // Do the work here
        $cache[md5($arg1)] = $results;
    }

    return $cache[md5($arg1)];
}

heredoc

不用再为引号发愁了

<?php
echo <<<EOM
  <div id="someblock">
    <img src="{$file}" />
  </div>
EOM;

把字符串"当作"数组

<?php
$str = 'hell o World';
echo $str; //outputs: "hell o World"

$str[0] = 'H';
echo $str; //outputs: "Hell o World"

$str[4] = null;
echo $str; //outputs: "Hello World"

快速注释/反注释一段代码

这个确实很有意思

<?php
//*
	echo 'blah';
	die('not commented');
//*/


/*
	echo 'blah';
	die('commented');
//*/

函数参数类型提示

相对于其他编程语言(java/c#),php这块比较弱,只能判断数组和对象

<?php
function foo ( array $param0, stdClass $param1 );

不用array_merge,而是使用'+’,使用更方便,也更易理解。在处理默认值和用户定义值时尤为方便。

<?php
// Set the normal defaults.
$control_defaults = array( 'type' => 'text', 'size' => 30 );

$control = $control_defaults + array( 'name' => 'surname', 'size' => 40 );

使用ob_start,关键是ob_start还支持callback,真是太贴心了。

<?php
function censor($text) {
	strtr($text, array('胡萝卜' => '***'));
}

ob_start(censor);
echo '小白兔最爱吃胡萝卜';
ob_end_flush();

//输出:小白兔最爱吃***

reflection,很强大,基本上能还原一个类的实际面貌。

ctype function

<?php
ctype_alnum //是否为字母和数字的组合
ctype_digit //是否为数字
ctype_alpha //是否为字母
...

快速向数组添加元素

如果是追加一个元素的话,推荐使用这种方法,而不是array_push

<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';

序列化/反序列化

<?php
serialize / unserialize

json支持

<?php
json_encode / json_decode

获取对象的类

<?php
get_class($obj)

动态调用函数/方法

<?php
call_user_func_array

错误/异常处理

<?php
set_error_handler / set_exception_handler

array_walk (via cloudchen)

array_walk() 能让结构化数据的处理逻辑更清晰. 实现接近js的传入函数as参数

<?php
function create_sth($walked_value, $key, $other_arg) {
	//...
}
function delete_sth(& $walked_value, $key, $other_arg) {
	//...
}

$contents = fetch_directory_contents($folder);

array_walk($contents, 'create_sth', $other_arg);
array_walk($contents, 'delete_sth', $other_arg); 

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK