Sebastian B.-Hagensen wrote on 19/03/2015 16:27:
Another way to unify array and string callback may be to use the
callable syntax and have it return a closure:
callable('strlen');
callable($object, $methodName);
callable('class', 'staticMethod')

Andrea proposed a slightly different syntax for function references a few months back - see withdrawn RFC [1] and accompanying Internals thread [2].

I actually quite like the callable() syntax. Whereas Andrea's suggestion was a parser rule that took a bare string, like &foo, this would have to always take a string, like callable('foo'), but that's good if we want to roll in dynamic callbacks as well:

Right now, we have to embed the type information in the variable name, because this is just a string:
$hook_callback = 'hook_' . $hook_name;
This has a much strong information scent:
$hook = callable('hook_' . $hook_name);

There's then the question of what kind of object it would return - a Closure? Some child or sibling of Closure? What methods could be usefully provided?

What should happen if the arguments provided aren't callable? Throw an exception?

Also, do we still need a way to delay testing of callability, to allow this:

class A {
    private function foo() { echo 'foo'; }
    public function bar(callable $call) { $call(); }
}
$a = new A;
// A::foo is not callable here, but will be when we get inside A::bar
var_dump(is_callable([$a, 'foo'] ));
$a->bar( [$a, 'foo'] );

Currently works: http://3v4l.org/pNZgn


[1] https://wiki.php.net/rfc/function_referencing
[2] http://marc.info/?t=140710275400001&r=2&w=2

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to