用error_log函数写缓存

恩,没错,@伪造 在看ezSQL源代码的时候,总是看到作者用error\_log函数写缓存,如:

error_log( serialize($result_cache), 3, $cache_file);

来看看error\_log的原型:

bool error_log ( string $message [, 
    int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

*error\_log() log types*

0

message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error\_log configuration directive is set to. This is the default option.

1

message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.

2

No longer an option.

3

message is appended to the file destination. A newline is not automatically added to the end of the message string.

4

message is sent directly to the SAPI logging handler.

当$message\_type 值为3时,代表写文件,而且是以append的方式写文件。同类型的函数有file\_put\_contents,不过它是以覆盖方式写文件。而使用这两个函数,都能节约代码,因为file\_put\_conents和error\_log不需要提前获得文件句柄,当然也不需要关闭它们。