On Wed, 27 Nov 2019 at 23:33, David Rodrigues <david.proweb@gmail.com> wrote:
>
> Hi internals,
>
> I am using the register_shutdown_function() inside a class, and then I have
> created a private method to be called during shutdown. I have noted that if
> the method is public it works, but private does not.
>
> This shutdown function should be private,
> to avoid execution at a public scope.
That is one of the reasons Closure::fromCallable was added:
https://www.php.net/manual/en/closure.fromcallable.php
So like: register_shutdown_function(Closure::fromCallable([$this, 'leave' ]));
Or in your example: https://3v4l.org/MSLA4
> spl_autoload_register([ self::class, 'privateMethod' ]); // OK
That is not OK. It might not give an error, but it's a bug if a
private method can be called accidentally, without jumping through the
deliberate hoops to work around private methods.
Claude Pache wrote:
> Another workaround is to use a closure:
Although that works, using Closure::fromCallable() has the benefits of
preserving the parameters and their info.
function foo(int $x) {
}
$fn = Closure::fromCallable('foo');
$reflection = new ReflectionFunction($fn);
foreach ($reflection->getParameters() as $param) {
echo "Param type is: " . $param->getType();
}
// Output is "Param type is: int"
cheers
Dan
Ack