作者归档:lgs

php call_user_func_array

call_user_func_array() 调用回掉函数,并把一个数组参数坐位回掉函数。也可以执行类中的方法。

例子:

<?php
function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2</br>";
}
class foo {
    static function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2</br>";
    }

    function food($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2</br>";
    }
}

call_user_func_array("foobar", array("one", "two"));

$foo = new foo;
call_user_func_array(array($foo, "food"), array("three", "four"));
//这种方式是错误的
//call_user_func_array(array("foo", "food"), array("three", "four"));
//下面这种办法,通过PHP的对象的方式调用一个静态方法,并不会报错。但是静态方法中不可以出现$this关键字
call_user_func_array(array($foo, "bar"), array("three", "four"));
//个人感觉调用静态方法还是这样会好些
call_user_func_array(array("foo", "bar"), array("three", "four"));
?>

结果:

foobar got one and two
foo::food got three and four
foo::bar got three and four
foo::bar got three and four

 

 

php restore_exception_handler

restore_exception_handler ()函数恢复PHP自身的异常处理。会清除set_exception_handler中用户自定义的函数处理方式

例子:

function handleException(Exception $e)
{ 
    do{
        echo "<b>错误信息</b>:" . $e->getMessage() . "</br>";
        echo "<b>错误码</b>:" . $e->getCode() . "</br>";
        echo "<b>错误文件</b>:" . $e->getFile() . "</br>";
        echo "<b>错误行数</b>:" . $e->getLine() . "</br>";
        echo "<b>错误行数</b>:" . $e->getPrevious() . "</br>";
        echo "<b>异常追踪信息</b>:</br>";
        echo "</pre>" . print_r($e->getTrace(), true) . "</pre></br>";
        echo "***********</br>";
    } while($e = $e->getPrevious());
}

set_exception_handler("handleException");

function test($name) {
    throw new Exception("Test exception", 1000);
}

restore_exception_handler();
test("codyi");

结果:

Fatal error: Uncaught exception 'Exception' with message 'Test exception' in /Users/liguosong/work/php/Testing/test.php on line 21 Exception: Test exception in /Users/liguosong/work/php/Testing/test.php on line 21 Call Stack: 0.0002 251616 1. {main}() /Users/liguosong/work/php/Testing/test.php:0 0.0003 252240 2. test(???) /Users/liguosong/work/php/Testing/test.php:25

 

 

php Exception 详解

Exception 类是php所有异常的基类。这个类包含如下方法:

__construct — 异常构造函数
getMessage — 获取异常消息内容
getPrevious — 返回异常链中的前一个异常
getCode — 获取异常代码
getFile — 获取发生异常的程序文件名称
getLine — 获取发生异常的代码在文件中的行号
getTrace — 获取异常追踪信息
getTraceAsString — 获取字符串类型的异常追踪信息

以上方法的简介直接从PHP官网抄来的。

下面写个例子:

function handleException(Exception $e)
{ 
    do{
        echo "<b>错误信息</b>:" . $e->getMessage() . "</br>";
        echo "<b>错误码</b>:" . $e->getCode() . "</br>";
        echo "<b>错误文件</b>:" . $e->getFile() . "</br>";
        echo "<b>错误行数</b>:" . $e->getLine() . "</br>";
        echo "<b>错误行数</b>:" . $e->getPrevious() . "</br>";
        echo "<b>异常追踪信息</b>:</br>";
        echo "</pre>" . print_r($e->getTrace(), true) . "</pre></br>";
        echo "***********</br>";
    } while($e = $e->getPrevious());
}

set_exception_handler("handleException");

class MyException extends Exception {}

function test($name) {
    throw new Exception("Test exception", 1000);
}

try{
    test("codyi");
} catch (Exception $ex) {
    throw new MyException("My exception", 1001, $ex);
}

输出结果:

错误信息:My exception
错误码:1001
错误文件:/Users/liguosong/work/php/Testing/test.php
错误行数:27
错误行数:exception 'Exception' with message 'Test exception' in /Users/liguosong/work/php/Testing/test.php:21 Stack trace: #0 /Users/liguosong/work/php/Testing/test.php(25): test('codyi') #1 {main}
异常追踪信息:
Array ( ) 
***********
错误信息:Test exception
错误码:1000
错误文件:/Users/liguosong/work/php/Testing/test.php
错误行数:21
错误行数:
异常追踪信息:
Array ( [0] => Array ( [file] => /Users/liguosong/work/php/Testing/test.php [line] => 25 [function] => test [args] => Array ( [0] => codyi ) ) ) 
***********

 

php set_error_handler

set_error_handler() 设置用户自定义的错误处理函数。在程序的运行期间,通过这个函数可以将错误信息按照我们自己的函数处理。

例子:

function handleError($errno, $errstr, $errfile, $errline)
{ 
    echo "<b>Custom error:</b> [$errno] $errstr<br />";
    echo " Error on line $errline in $errfile<br />";
}

set_error_handler("handleError");

$test = 2;

if ($test>1)
{
    trigger_error("A custom error has been triggered");
}

结果:

Custom error: [1024] A custom error has been triggered
Error on line 14 in /Users/liguosong/work/php/Testing/test.php

 

php restore_error_handler

restore_error_handler()函数恢复PHP自身的错误处理程序。从下面的例子中,可以看出,当执行restore_error_handler()函数之后,我们自己定义的错误处理函数已经不在起作用了。

例子:

function handleError($errno, $errstr, $errfile, $errline)
{ 
    echo "<b>Custom error:</b> [$errno] $errstr<br />";
    echo " Error on line $errline in $errfile<br />";
}

set_error_handler("customError");

$test = 2;

if ($test>1)
{
    trigger_error("A custom error has been triggered");
}

//恢复PHP本身的错误处理。正常显示trigger信息
restore_error_handler();

if ($test > 1)
{
    trigger_error("A custom error has been triggered");
}

结果:

Custom error: [1024] A custom error has been triggered
Error on line 14 in /Users/liguosong/work/php/Testing/test.php
Notice: A custom error has been triggered in /Users/liguosong/work/php/Testing/test.php on line 22 Call Stack: 0.0096 253080 1. {main}() /Users/liguosong/work/php/Testing/test.php:0 0.0100 253952 2. trigger_error(???) /Users/liguosong/work/php/Testing/test.php:22

 

php strstr

strstr()函数返回字符串中的一部分。

strstr(string,search,
string:必须,规定被搜索的字符串
search:必须,搜索字符串
before_needle:可选,默认值是false。如果是true,从被搜索字符串往前开始截取。如果是false,从搜索字符串开始,一直到结尾

例子:

echo strstr("Hello world.", "w");

结果:

world.

例子:

echo strstr("Hello world.", "w", true);

结果:

Hello

 

)

 

memcached分布式解决方案

memcache缓存是缓存数据库查询结果减轻数据库压力。这里不探讨memcache如何使用。

很多时候一台memcached服务器不能满足需求,就需要多台memcached服务器。但是用了多台服务器,程序怎么知道把数据存储到哪台服务器,又怎么知道去哪台服务器读取数据呢? 继续阅读

python hello world

简介:
官方介绍是:
Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。它简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。 继续阅读

升级php版本到php5.5,掉坑了。。。。

昨天刚刚升级了系统跟php的版本。把php升级到了5.5。升完级后,感觉挺爽。

结果一会服务器就出现了502的错误,有些懵了。。。后来经过排查,发现原来ngnix的一个配置不管用了。就是fastcgi_pass。原来是转到一个9000的端口上。升级后,可能php的配置文件变了。这个不管用了。 继续阅读

php5.4 新特性 trait

php5.4中的trails,是新引入的特性。目的是为了有得场合理的让代码实现重用。

注意:trails不是类,不能实例化。如果trails的方法名和类中的方法名冲突,类中的方法名会覆盖trails的方法名。说明类中的方法名,优先级更高。 继续阅读