Skip to content

set_error_handler and set_exception_handler do not necessarily return a callable #3421

@kkmuffme

Description

@kkmuffme

But just an array, depending on the callable used and the way it was called.
e.g. 2 examples where we don't get a callable but an array (and therefore get an error):

<?php

function accept_callable(callable $arg) {}

class Foo {
        public function __construct() {
                set_error_handler([__CLASS__, 'log_error']);
                set_exception_handler([$this, 'log_exception']);
        }

        public function log_error( $type, $message, $file, $line ) {
                echo "log error" . PHP_EOL;
                return true;
        }

        public function log_exception( $e ) {
                echo "log exception" . PHP_EOL;
                var_dump($e);
        }
}

$foo = new Foo();

$previousHandler = set_error_handler(static fn () => false);
restore_error_handler();

accept_callable($previousHandler);

https://3v4l.org/rjRSm


<?php

function accept_callable(callable $arg) {}

class Foo {
        public function __construct() {
                set_error_handler([$this, 'log_error']);
                set_exception_handler([$this, 'log_exception']);
        }

        private function log_error( $type, $message, $file, $line ) {
                echo "log error" . PHP_EOL;
                return true;
        }

        public function log_exception( $e ) {
                echo "log exception" . PHP_EOL;
                var_dump($e);
        }
}

$foo = new Foo();

$previousHandler = set_error_handler(static fn () => false);
restore_error_handler();

accept_callable($previousHandler);

https://3v4l.org/B7Et7

If you look at the php-src you see that the return type isn't MAY_BE_CALLABLE either, so this is not a bug in PHP but an issue in the docs only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions