diff --git a/language/types/callable.xml b/language/types/callable.xml index 42665a778d62..fa6065ec292a 100644 --- a/language/types/callable.xml +++ b/language/types/callable.xml @@ -30,10 +30,18 @@ A method of an instantiated object is passed as an array containing an object at index 0 and the - method name at index 1. Accessing protected and private methods from - within a class is allowed. + method name at index 1. Passing protected and private methods + with the callable syntax is allowed. + + + If a callable of a protected or a private method is invoked from outside the class + with no visibility of said methods (e.g. being called from a POSIX signal handler), + a runtime error is thrown because the callable is invalid at the time of invocation. + + + Static class methods can also be passed without instantiating an object of that class by either, passing the class name @@ -156,6 +164,80 @@ print implode(' ', $new_numbers); + + + + Manually invoking callbacks + + + $x * 3, 24); + +// define a class implementing the __invoke method +class NumberTripler +{ + public function __invoke($x) + { + return $x * 3; + } +} + +// prints "15" +$tripler = new NumberTripler(); +processNumber($tripler, 5); + +// define a class with a private instance method, to be called with the callable syntax +class HiddenFactory +{ + private function hiddenWork() + { + echo "256" . PHP_EOL; + } + + public function getProof() + { + return Closure::fromCallable([$this, 'hiddenWork']); + } +} + +// prints "256" +$factory = new HiddenFactory(); +$theProof = $factory->getProof(); +invokeCallback($theProof); +?> + + &example.outputs; + + + + + + ¬e.func-callback-exceptions;