Nasıl set_error_handler yapabilirsiniz () bir nesne üzerinde bir yöntemini çağırmak?

3 Cevap

Ben bir yerde (bir dosyaya günlüğü) olarak PHP hataları en işlemek için PHP set_error_handler () işlevi kullanarak düşünüyorum. I set_error_handler () bir işlev adı geçebilir gibi belgelere görünüyor. Güzel! Ama ben güzel bir giriş yöntemi vardır bir ErrorManager nesne var. Ben ErrorManager nesne kullanmak ve bunun için bir özel hata işleyicisi yöntemi yazmak istiyorum, ve bu ErrorManager diyoruz set_error_handler var.

Ben sadece böyle bir şey yapabileceğini

set_error_handler($this->customErrorHandler);

? Ya da geçersiz olurdu?

3 Cevap

Çağrılacak nesne ve yöntem adı bir dizi Pass:

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

set_error_handler() takes a callback,

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.

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().

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

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.

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.

(Vurgu eklenmiştir)

PHP 5.3 'te bir kapatma bunu yapabilirdi:

set_error_handler( function() { $this->customErrorHandler(); } );