如何使set_error_handler()调用对象上的方法? [英] How can I make set_error_handler() call a method on an object?

查看:160
本文介绍了如何使set_error_handler()调用对象上的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我考虑使用PHP中的set_error_handler()功能在一处处理大多数PHP错误(将它们记录到文件中).从文档看来,我是否可以将函数名称传递给set_error_handler().好的!但是我有一个具有不错的日志记录方法的ErrorManager对象.我想使用该ErrorManager对象并为其编写一个特殊的错误处理程序方法,并让set_error_handler调用该ErrorManager.

I think about using the set_error_handler() functionality in PHP to handle most of the PHP errors in one place (logging them to a file). From the documentation it looks like if I can pass a function name to set_error_handler(). Nice! But I have an ErrorManager object which has a nice logging method. I want to use that ErrorManager object and write an special error handler method for it, and have set_error_handler call that ErrorManager.

我可以做点什么吗?:

set_error_handler($this->customErrorHandler);

还是无效的?

推荐答案

传入对象数组和要调用的方法名称:

Pass in an array of the object and the method name to be called:

set_error_handler(array($this, 'customErrorHandler'));

set_error_handler() 需要

一些函数,例如call_user_func() 或usort()接受用户定义 回调函数作为参数. 回调函数不仅可以 简单的功能,也是对象 方法,包括静态类 方法.

Some functions like call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods.

PHP函数通过其名称传递 作为字符串.任何内置或 可以使用用户定义的功能, 语言构造除外,例如: array(),echo(),empty(),eval(), exit(),isset(),list(),print()或 unset().

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo(), empty(), eval(), exit(), isset(), list(), print() or unset().

实例化对象的方法是 作为包含 索引为0的对象和方法名称 在索引1处.

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

静态类方法也可以是 在没有实例化对象的情况下通过 通过班级而获得该班级的 名称而不是索引0处的对象.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.

除了常见的用户定义 函数,create_function()也可以 用于创建匿名 回调函数.从PHP 5.3.0开始 也可以将闭包传递给 回调参数.

Apart from common user-defined function, create_function() can also be used to create an anonymous callback function. As of PHP 5.3.0 it is possible to also pass a closure to a callback parameter.

(添加了重点)

这篇关于如何使set_error_handler()调用对象上的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆