wt., 11 lut 2020, 18:42 użytkownik Manuel Canga <manuelca...@gmail.com>
napisał:

> On Tue, 11 Feb 2020 at 17:49, Dan Ackroyd <dan...@basereality.com> wrote:
> >
> > Nicolas Grekas wrote:
> > > I wish this would return a Closure instead, making $foo::function the
> > > equivalent of Closure::fromCallable($foo).
> >
> > I didn't include the following in that RFC, because I thought it would
> > be too controversial, but I think it's worth considering a new syntax
> > for this.
> >
> > Given the code:
> >
> > function foo();
> > class Zoq {
> >     public function Fot() {}
> >     public static function Pik() {}
> > }
> > $obj = new Zoq();
> >
> >
> > Then these:
> >
> > $(foo);
> > $($obj, Fot);
> > $(Zoq, Fot);
> >
> > Would be equivalent to:
> >
> > Closure::fromCallable('foo');
> > Closure::fromCallable([$obj, 'Fot']);
> > Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq',
> 'Fot']);
> >
> > Or similar.
> >
> > The justification for having a dedicated syntax is that I think
> > readability is quite important in code, and it's reasonably common for
> > me to have quite long lists of 'callables'.
> >
> > [Bar::class, foo1::function],
> > [Bar::class, foo2::function],
> > [Bar::class, foo3::function],
> > [Bar::class, foo4::function]
> >
> > vs
> >
> > $(Bar, foo1),
> > $(Bar, foo2),
> > $(Bar, foo3),
> > $(Bar, foo4)
> >
> > The latter is far easier to read for me.
> >
> > Nikita Popov wrote:
> > > This would circumvent all the issues outlined in
> > > https://wiki.php.net/rfc/consistent_callables.
> >
> > Probably there would still be some issues with some of the weird stuff
> > happening internally in SPL related code where the deep horrors
> > rest...but we can leave them alone...and they might not wake.
> >
> > cheers
> > Dan
> > Ack
>
> Other option:
>
> Closure::fromCallable('foo');
> Closure::fromCallable([$obj, 'Fot']);
> Closure::fromCallable('Zoq::Fot'); or Closure::fromCallable(['Zoq',
> 'Fot']);
>
> to
>
> <Foo>
> <$foo, Fot>
> <Zoq::Fot>
> <Zoq,'Fot>
>
> E.g:
>
> array_map(<strlen>, $array);
> array_map(<I18n, Translate>, $array);
>
> Do you like ?
>

That reminds me an old draft RFC https://wiki.php.net/rfc/short-closures
where I thought curly braces can be used to create closure from syntax
nearly the same as invoking but without parentheses.

That way we can provide clear intent - I mean if whatever is around: curly
braces or $ with parentheses IMHO it should be consistent with call like
format. For example:

$(strlen) or {strlen} for Closure::fromCallable('foo')

$($foo->Fot) or {$foo->Fot} for Closure::fromCallable([$obj, 'Fot'])

$(Zoq::Fot) or {Zoq::Fot} for Closure::fromCallable('Zoq::Fot')

Etc. The same inside like a call but wrapped in something and with no
parentheses part.

Cheers,
--
Michał Brzuchalski

>

Reply via email to