# [RFC][VOTE] Closure from callable

- Thread: https://externals.io/message/93342
- Original archive: https://news-web.php.net/php.internals/93342
- Started by Dan Ackroyd on 2016-05-15T17:29:22+00:00
- Messages: 8
- RFC: Closure from callable function (Implemented (PHP 7.1)) — https://externals.io/rfc/closurefromcallable.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #93342 — Dan Ackroyd — 2016-05-15T17:29:22+00:00

URL: https://externals.io/message/93342#93342

Hello,

I've opened the voting for the Closure from callable RFC -
https://wiki.php.net/rfc/closurefromcallable

Just to note, some people I've spoken to have expressed a desire to
have a more powerful syntax where 'bare' function names are used,
like:

callable(someFunctionName)
callable($this->method)

I fully agree with those people, however I can't see anyway to do that
before PHP 8. It would almost certainly need some clean up of the
allowed syntax to disambiguate what `$this->method` means for:

class Foo {
    public $method;
    public function method() { }
}

Leaving that top-level function name available for future versions,
where we might be able to support it properly, is one of the reasons
to use the more verbose function name.

cheers
Dan

---

## #93343 — Nikita Nefedov — 2016-05-15T18:20:42+00:00

URL: https://externals.io/message/93342#93343 · In reply to: #93342

> 
> 
> Hello,
> 
> I've opened the voting for the Closure from callable RFC -
> https://wiki.php.net/rfc/closurefromcallable
> 
> Just to note, some people I've spoken to have expressed a desire to
> have a more powerful syntax where 'bare' function names are used,
> like:
> 
> callable(someFunctionName)
> callable($this->method)
> 
> I fully agree with those people, however I can't see anyway to do that
> before PHP 8. It would almost certainly need some clean up of the
> allowed syntax to disambiguate what `$this->method` means for:
> 
> class Foo {
>   public $method;
>   public function method() { }
> }
> 
> Leaving that top-level function name available for future versions,
> where we might be able to support it properly, is one of the reasons
> to use the more verbose function name.

Hey Dan,

why would you need to support a $this->fieldName case though? If it's a field it probably already contains a closure (or, well, if you need to make a closure from arbitrary callable we could have a Closure::fromCallable named ctr). Instead of making a function 'callable()', we could make a language construct:

   expr:
       ...
       | `T_CALLABLE` '(' callable_expr ')'

   callable_expr:
       '$this->' function_name
       | class_name '::' function_name
       | function_name

This would have an advantage of being statically checked (hence easier refactoring in IDEs).

I see merging symbol tables as a pretty big of a deal for too much people, this thing alone could easily create another case of Python 3 syndrome.

---

## #93345 — Sara Golemon — 2016-05-16T00:56:38+00:00

URL: https://externals.io/message/93342#93345 · In reply to: #93343

> why would you need to support a $this->fieldName case though?
>
Because to not support it would be to deliberately design in a new
flavor of inconsistency into the language. $obj->memb is a property
access in PHP.  Making it suddenly mean "method" is a significant
change (and one which belongs in a major version if at all).

-Sara

---

## #93349 — Dmitry Stogov — 2016-05-16T08:31:22+00:00

URL: https://externals.io/message/93342#93349 · In reply to: #93342

I'm sorry, I didn't follow the RFC discussion.
In general I like the idea, but why not to use Closure constructor?

$f = new Closure("some_func");

would look more readable than

$f = Closure::fromCallable("some_func");

Sorry, if this was already discussed before.

Thanks. Dmitry.

---

## #93350 — Marco Pivetta — 2016-05-16T08:34:59+00:00

URL: https://externals.io/message/93342#93350 · In reply to: #93349

> I'm sorry, I didn't follow the RFC discussion.
> In general I like the idea, but why not to use Closure constructor?
>
> $f = new Closure("some_func");
>
> would look more readable than
>
> $f = Closure::fromCallable("some_func");
>
> Sorry, if this was already discussed before.
>
> Thanks. Dmitry.
>

Constructors can't be used as callbacks, so you wouldn't be able to map
this operation without yet another level of indirection.

A named constructor also leaves more space for later improvements, and is
quite a bit more expressive.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #93351 — Nikita Nefedov — 2016-05-16T08:40:42+00:00

URL: https://externals.io/message/93342#93351 · In reply to: #93345

> On Sun, May 15, 2016 at 11:20 AM, Nikita Nefedov <inefedor@gmail.com>  
> wrote:
> > […]
> Because to not support it would be to deliberately design in a new
> flavor of inconsistency into the language. $obj->memb is a property
> access in PHP.  Making it suddenly mean "method" is a significant
> change (and one which belongs in a major version if at all).
>
> -Sara

The whole idea is not in the syntax here, but in the notion
that this access can only be static (so there'd be no way to
do `$cb = [$this, "methodName"]; callable($cb)` - you'd have
to call `Closure::fromCallable($cb)` instead).

If you don't like the fact that `$obj->methodName` looks like
a field access then there are ways around this.
Because we still need a static way of exporting function
references (whether it'd be as a closure or array). Other
proposals were using `$obj->method::FUNCTION` syntax or something
similar for example...

---

## #93362 — Dan Ackroyd — 2016-05-16T20:12:35+00:00

URL: https://externals.io/message/93342#93362 · In reply to: #93349

> I'm sorry, I didn't follow the RFC discussion.
> In general I like the idea, but why not to use Closure constructor?
>

Hi Dmitry,

Several small to medium sized reasons that make me think it's the best way.

As Marco said, constructors cannot be passed as callbles, which for
whatever reason both myself and he seem to encounter as a problem more
than the average programmer. I had hoped to address this with this
RFC: https://wiki.php.net/rfc/callableconstructors - but the feedback
on that was massively negative so far.

Second, it's quite likely we're going to have other ways of creating
closures in the future. Having a default constructor that is 'special'
above the other named constructors is a source of API sadness for me.

> would look more readable than

I actually seem to like named constructors more than default
constructors, and am totally used to reading them. That may be down to
my dislike of how constructors have special rules in being a little
bit static, and also being instance methods at the same time.

cheers
Dan

---

## #93699 — Dan Ackroyd — 2016-06-01T21:26:27+00:00

URL: https://externals.io/message/93342#93699 · In reply to: #93343

> 
> Hello,
>
> I've opened the voting for the Closure from callable RFC -
> https://wiki.php.net/rfc/closurefromcallable

Voting was closed, the RFC has passed 40-0.

Thanks again to Bob, for helping with (aka doing most of) the code.

cheers
Dan
