dongyounai6281 2019-03-16 17:30
浏览 48
已采纳

set_error_handler with PRIVATE-static-method callback

Is it possible to use private static method as a set_error_hander callback?

This works fine:

<?php
    class DisplayErrors{

        private static $email_address = 'myemail@mail.com';

        public static function error_handler($number, $message, $file, $line, $vars) {
            // send error to email;
        }

        private static function toEmail(){
            // set_error_handler('self::error_handler');
            set_error_handler(__CLASS__ .'::error_handler');
        }

    }

This has no effect nor error:

<?php
    class DisplayErrors{

        private static $email_address = 'myemail@mail.com';

        private static function error_handler($number, $message, $file, $line, $vars) {
            // changed to private static method;
        }

        private static function toEmail(){
            // set_error_handler('self::error_handler');
            set_error_handler('self::error_handler');
        }

    }

I have also tried:

set_error_handler(array('self','error_handler'));
// no effect no error
set_error_handler('&self::error_handler');
// invalid callback
  • 写回答

1条回答 默认 最新

  • doumaji6215 2019-03-16 18:19
    关注

    If you're using PHP 7.1 or higher, the new closure::fromCallable method will allow you to do this.

        private static function toEmail(){
            set_error_handler(Closure::fromCallable('static::error_handler'));
        }
    

    Closure::fromCallable() evaluates the callable argument in the scope where it's called, so it can accept private and protected methods when used within the class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退