# RFC: alternative callback syntax

- Thread: https://externals.io/message/63124
- Original archive: https://news-web.php.net/php.internals/63124
- Started by Steve Clay on 2012-09-19T02:48:06+00:00
- Messages: 17
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #63124 — Steve Clay — 2012-09-19T02:48:06+00:00

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

Hello,

https://wiki.php.net/rfc/alternative_callback_syntax is a proposal for a simple 
alternative syntax for creating function callbacks.

The basics: Given a function/method call, replace the argument list with "::function". PHP 
would evaluate this as a callback for the function/method, fully resolving any 
class/function names according to the current namespace and any use statements.

In my opinion this syntax would aid readability and benefit users via fewer typos and 
namespace errors, and via (hopefully) better IDE support.


I welcome any feedback. I don't have adequate C skills to offer a patch at the moment, but 
I generally read everything I can get my hands on about how PHP features are implemented 
and am not afraid to jump in.

Thanks for your time.

P.S. I've kicked around this idea around for awhile, but Ralph Schindler's idea for using 
::keyword provided a better path to BC, and more attractive/meaningful syntax.

P.P.S. I'm unaware if it's customary to throttle the release of RFCs, so I apologize for 
hogging any attention away from those under discussion.

Steve Clay
-- 
http://www.mrclay.org/

---

## #63127 — Sebastian Krebs — 2012-09-19T06:37:43+00:00

URL: https://externals.io/message/63124#63127 · In reply to: #63124

2012/9/19 Steve Clay <steve@mrclay.org>

> Hello,
>
> https://wiki.php.net/rfc/**alternative_callback_syntax<https://wiki.php.net/rfc/alternative_callback_syntax>is a proposal for a simple alternative syntax for creating function
> callbacks.
>
> The basics: Given a function/method call, replace the argument list with
> "::function". PHP would evaluate this as a callback for the
> function/method, fully resolving any class/function names according to the
> current namespace and any use statements.
>
> In my opinion this syntax would aid readability and benefit users via
> fewer typos and namespace errors, and via (hopefully) better IDE support.
>
>
> I welcome any feedback. I don't have adequate C skills to offer a patch at
> the moment, but I generally read everything I can get my hands on about how
> PHP features are implemented and am not afraid to jump in.
>
> Thanks for your time.
>
> P.S. I've kicked around this idea around for awhile, but Ralph Schindler's
> idea for using ::keyword provided a better path to BC, and more
> attractive/meaningful syntax.
>
> P.P.S. I'm unaware if it's customary to throttle the release of RFCs, so I
> apologize for hogging any attention away from those under discussion.
>
> Steve Clay


Hi,

Beside that I like the idea to get rid of defining callbacks as string (for
the same reason I like 'Classname::class' for classes) I don't like, that
functions will look like classes with a static property "function" then ...

Regards,
Sebastian


>
> --
> http://www.mrclay.org/
>
> --
> 
> 
>
>


-- 
github.com/KingCrunch

---

## #63133 — Steve Clay — 2012-09-19T13:15:28+00:00

URL: https://externals.io/message/63124#63133 · In reply to: #63127

> 2012/9/19 Steve Clay <steve@mrclay.org <mailto:steve@mrclay.org>>
> > […]

> reason I like 'Classname::class' for classes) I don't like, that functions will look like
> classes with a static property "function" then ...

FWIW, here are the syntaxes I've considered:

func_name; // parser can't distinguish from constant

callablefor func_name; // adds reserved word, token

func_name::callable; // adds reserved word

func_name::function; // in RFC

Steve
-- 
http://www.mrclay.org/

---

## #63134 — Ivan Enderlin @ Hoa — 2012-09-19T13:26:08+00:00

URL: https://externals.io/message/63124#63134 · In reply to: #63133

> 
> > […]
>
> > […]
>
> FWIW, here are the syntaxes I've considered:
>
> func_name; // parser can't distinguish from constant
>
> callablefor func_name; // adds reserved word, token
>
> func_name::callable; // adds reserved word
callable is already a reserved word (T_CALLABLE).

>
> func_name::function; // in RFC
>
> Steve

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #63155 — Levi Morrison — 2012-09-19T17:16:46+00:00

URL: https://externals.io/message/63124#63155 · In reply to: #63124

>
> Hello,
>
> https://wiki.php.net/rfc/alternative_callback_syntax is a proposal for a simple alternative syntax for creating function callbacks.
>
> The basics: Given a function/method call, replace the argument list with "::function". PHP would evaluate this as a callback for the function/method, fully resolving any class/function names according to the current namespace and any use statements.
>
> In my opinion this syntax would aid readability and benefit users via fewer typos and namespace errors, and via (hopefully) better IDE support.
>
>
> I welcome any feedback. I don't have adequate C skills to offer a patch at the moment, but I generally read everything I can get my hands on about how PHP features are implemented and am not afraid to jump in.
>
> Thanks for your time.
>
> P.S. I've kicked around this idea around for awhile, but Ralph Schindler's idea for using ::keyword provided a better path to BC, and more attractive/meaningful syntax.
>
> P.P.S. I'm unaware if it's customary to throttle the release of RFCs, so I apologize for hogging any attention away from those under discussion.
>
> Steve Clay
> --
> http://www.mrclay.org/
>
> --
> 
> 
>

I do not like this proposal.  The syntax is ugly and not intuitive.
We need a different way to accomplish this.

Levi Morrison

---

## #63157 — Andrew Faulds — 2012-09-19T17:19:18+00:00

URL: https://externals.io/message/63124#63157 · In reply to: #63155

> 
> > […]
> I do not like this proposal.  The syntax is ugly and not intuitive.
> We need a different way to accomplish this.
I completely agree. How about &function_name? Resembles C (function 
pointers!), and unless I'm getting confused, it isn't currently legal 
syntax for something else.

-- 
Andrew Faulds
http://ajf.me/

---

## #63160 — Steve Clay — 2012-09-19T17:41:08+00:00

URL: https://externals.io/message/63124#63160 · In reply to: #63134

> callable is already a reserved word (T_CALLABLE).

Oh, good. It's not listed here http://php.net/manual/en/tokens.php

Steve
-- 
http://www.mrclay.org/

---

## #63165 — Steve Clay — 2012-09-19T17:56:28+00:00

URL: https://externals.io/message/63124#63165 · In reply to: #63157

> I completely agree. How about &function_name? Resembles C (function pointers!), and unless
> I'm getting confused, it isn't currently legal syntax for something else.

$cb = & Foo::doSomething;
$cb = & foo_doSomething;
$cb = & $obj->doSomething;

The last looks like a reference to a property, though having verb method names would help 
distinguish. Not sure if the ambiguity would make this difficult to implement.

Some other ideas:

$cb = (callable) $obj->bar;
$cb = callable $obj->bar;
$cb = callable::$obj->bar;
$cb = $obj->bar::callable;

Steve
-- 
http://www.mrclay.org/

---

## #63166 — Andrew Faulds — 2012-09-19T18:01:02+00:00

URL: https://externals.io/message/63124#63166 · In reply to: #63165

> 
> > […]
>
> $cb = & Foo::doSomething;
> $cb = & foo_doSomething;
> $cb = & $obj->doSomething;
>
> The last looks like a reference to a property, though having verb 
> method names would help distinguish. Not sure if the ambiguity would 
> make this difficult to implement.
Oops, good point!
>
> Some other ideas:
>
> $cb = (callable) $obj->bar;
Ah, but (callable) that won't work for global functions, since 
(callable) is a cast, and if I try to do:

     $cb= (callable) mysql_real_escape_string;

I'd actually try to access a constant named 'mysql_real_escape_string' 
(bad!!)
> $cb = callable $obj->bar;
> $cb = callable::$obj->bar;
> $cb = $obj->bar::callable;
Here's two other possibilities from me:

     $cb = {mysql_real_escape_string};
     $cb = &{mysql_real_escape_string};

Both are completely illegal just now, I think.
>
> Steve


-- 
Andrew Faulds
http://ajf.me/

---

## #63169 — Steve Clay — 2012-09-19T18:21:51+00:00

URL: https://externals.io/message/63124#63169 · In reply to: #63166

> > […]
> Ah, but (callable) that won't work for global functions, since (callable) is a cast, and

(callable) is not usable as a cast (at least in 5.4.7):

$a = (callable)'str'; // Parse error: syntax error, unexpected 'callable' (T_CALLABLE)

This could work, and it makes sense to me at least.


>      $cb = {mysql_real_escape_string};
>      $cb = &{mysql_real_escape_string};

I'll add these possibilities to the RFC soon.

Steve
-- 
http://www.mrclay.org/

---

## #63170 — Andrew Faulds — 2012-09-19T18:23:01+00:00

URL: https://externals.io/message/63124#63170 · In reply to: #63169

> 
> > […]
>
> (callable) is not usable as a cast (at least in 5.4.7):
>
> $a = (callable)'str'; // Parse error: syntax error, unexpected 
> 'callable' (T_CALLABLE)
>
> This could work, and it makes sense to me at least.
Yes, the problem is that it would add a special syntax case, where 
(cast)identifier, in this case, uses a function name, not an expression. 
This is bad.

-- 
Andrew Faulds
http://ajf.me/

---

## #63184 — Ivan Enderlin @ Hoa — 2012-09-20T08:09:38+00:00

URL: https://externals.io/message/63124#63184 · In reply to: #63160

> 
> > […]
>
> Oh, good. It's not listed here http://php.net/manual/en/tokens.php
Exactly. Who is responsible to update the documentation? Julien?

Best regards.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #63185 — Pierre Joye — 2012-09-20T08:17:58+00:00

URL: https://externals.io/message/63124#63185 · In reply to: #63184

hi,

On Thu, Sep 20, 2012 at 10:09 AM, Ivan Enderlin @ Hoa
<ivan.enderlin@hoa-project.net> wrote:
> 
> > […]
>
> Exactly. Who is responsible to update the documentation? Julien?

Open a bug at bugs.php.net or http://edit.php.net/ or post on the
php-doc mailing list :)

Cheers,
-- 
Pierre

@pierrejoye

---

## #63186 — Ivan Enderlin @ Hoa — 2012-09-20T08:24:56+00:00

URL: https://externals.io/message/63124#63186 · In reply to: #63169

> 
> > […]
>
> (callable) is not usable as a cast (at least in 5.4.7):
>
> $a = (callable)'str'; // Parse error: syntax error, unexpected 
> 'callable' (T_CALLABLE)
>
> This could work, and it makes sense to me at least.
>
>
> > […]
These notations are just horrible.

PHP has recently introduced ::class to resolve a classname (namespace 
Foo; A::class // Foo\A). The “::” was originally a resolution operator 
for calls, but now, it is also used for names (which sounds good for 
me). Here, you propose the new notation ::function, which can be 
understood as: “resolve this function or method as a callable”. We stay 
in a resolution context. It is consistent regarding to the evolution of 
this operator.

Another consistent notation is the cast: (callable) $obj->bar;.

However, having { } around with & is absolutely horrible. It is an awful 
blend between references and dynamic variables, it is not consistent and 
error-prone when reading.

Cheers :-).

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #63187 — Adam Harvey — 2012-09-20T09:05:24+00:00

URL: https://externals.io/message/63124#63187 · In reply to: #63186

On 20 September 2012 16:24, Ivan Enderlin @ Hoa
<ivan.enderlin@hoa-project.net> wrote:
>
> > […]
>
> These notations are just horrible.

Agreed. They're very line noisey and non-obvious.

> PHP has recently introduced ::class to resolve a classname (namespace Foo;
> A::class // Foo\A). The “::” was originally a resolution operator for calls,
> but now, it is also used for names (which sounds good for me). Here, you
> propose the new notation ::function, which can be understood as: “resolve
> this function or method as a callable”. We stay in a resolution context. It
> is consistent regarding to the evolution of this operator.

I like ::callable, personally, since it suggests the resulting type,
but ::function would also be OK by me.

> Another consistent notation is the cast: (callable) $obj->bar;.

I think the analogy with ::class is stronger than the analogy with
type casting (since it's not a variable or literal), although I'm not
really too fussed either way.

In terms of the feature itself (since I haven't weighed in yet), I
think it's a reasonable piece of syntactic sugar, although I'd
definitely want to see a patch before voting. :)

Adam

---

## #63188 — Pete Boere — 2012-09-20T09:30:48+00:00

URL: https://externals.io/message/63124#63188 · In reply to: #63187

Tentative +1 for the func_name::callable version.

callable seems more generic, and possibly more intuitive when scan reading
code that contains lots of 'function'.

class A {
    static function init() {
        $inst = new self;
        var_export <http://www.php.net/var_export>($inst->bar::callable);
        $inst->bar();
    }
    function bar() {
        var_export <http://www.php.net/var_export>(B::baz::callable);
    }}





-- 
Pete Boere
Web Developer

---

## #63199 — Ivan Enderlin @ Hoa — 2012-09-20T11:18:26+00:00

URL: https://externals.io/message/63124#63199 · In reply to: #63185

> hi,
>
> On Thu, Sep 20, 2012 at 10:09 AM, Ivan Enderlin @ Hoa
> <ivan.enderlin@hoa-project.net> wrote:
> > […]
> Open a bug at bugs.php.net or http://edit.php.net/ or post on the
> php-doc mailing list :)
Patch has been proposed through http://edit.php.net.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/
