# Are there plans to allow anonymous functions as callbacks?

- Thread: https://externals.io/message/43240
- Original archive: https://news-web.php.net/php.internals/43240
- Started by Richard Quadling on 2009-03-04T11:33:32+00:00
- Messages: 5
- 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

---

## #43240 — Richard Quadling — 2009-03-04T11:33:32+00:00

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

Hi.

Quite a simple question (assuming I've got the terminology correct).

Are there any plans to allow code like this (amended example taken
from the `array_map()` documentation) ...

<?php
$a = array(1, 2, 3, 4, 5);
$b = array_map(function($n){return($n * $n * $n)}, $a);
print_r($b);

For me, this is cleaner than having a use-once, namespace-polluting function.

Currently, this is producing a parse error.


A real alternative though would be to allow nested functions...

<?php
function cube_array($a) {
  function cube($n) {
    return($n * $n * $n);
  }
  return array_map("cube", $a);
}
$a = array(1, 2, 3, 4, 5);
$b = cube_array($a);
print_r($b);

Ideally, cube() would not be visible outside of cube_array(). Scoped functions.

Regards,

Richard Quadling.
-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43241 — Alexey Zakhlestin — 2009-03-04T11:42:34+00:00

URL: https://externals.io/message/43240#43241 · In reply to: #43240

On Wed, Mar 4, 2009 at 2:33 PM, Richard Quadling
<rquadling@googlemail.com> wrote:
> Hi.
>
> Quite a simple question (assuming I've got the terminology correct).
>
> Are there any plans to allow code like this (amended example taken
> from the `array_map()` documentation) ...
>
> <?php
> $a = array(1, 2, 3, 4, 5);
> $b = array_map(function($n){return($n * $n * $n)}, $a);
> print_r($b);
>
> For me, this is cleaner than having a use-once, namespace-polluting function.
>
> Currently, this is producing a parse error.

It works. You have a typo. You need to put ";" after return (…)

>
>
> A real alternative though would be to allow nested functions...
>
> <?php
> function cube_array($a) {
>  function cube($n) {
>    return($n * $n * $n);
>  }
>  return array_map("cube", $a);
> }
> $a = array(1, 2, 3, 4, 5);
> $b = cube_array($a);
> print_r($b);
>
> Ideally, cube() would not be visible outside of cube_array(). Scoped functions.
>
> Regards,
>
> Richard Quadling.
> --
> -----
> Richard Quadling
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> "Standing on the shoulders of some very clever giants!"
>
> --
> 
> 
>
>



-- 
Alexey Zakhlestin
http://www.milkfarmsoft.com/

---

## #43242 — Ionut G. Stan — 2009-03-04T12:07:52+00:00

URL: https://externals.io/message/43240#43242 · In reply to: #43240

You need PHP 5.3 for that. Here's the RFC:

http://wiki.php.net/rfc/closures



> Hi.
>
> Quite a simple question (assuming I've got the terminology correct).
>
> Are there any plans to allow code like this (amended example taken
> from the `array_map()` documentation) ...
>
> <?php
> $a = array(1, 2, 3, 4, 5);
> $b = array_map(function($n){return($n * $n * $n)}, $a);
> print_r($b);
>
> For me, this is cleaner than having a use-once, namespace-polluting function.
>
> Currently, this is producing a parse error.
>
>
> A real alternative though would be to allow nested functions...
>
> <?php
> function cube_array($a) {
>    function cube($n) {
>      return($n * $n * $n);
>    }
>    return array_map("cube", $a);
> }
> $a = array(1, 2, 3, 4, 5);
> $b = cube_array($a);
> print_r($b);
>
> Ideally, cube() would not be visible outside of cube_array(). Scoped functions.
>
> Regards,
>
> Richard Quadling.

-- 
Ionut G. Stan
I'm under construction  |  http://igstan.blogspot.com/

---

## #43243 — Olivier B. — 2009-03-04T12:16:37+00:00

URL: https://externals.io/message/43240#43243 · In reply to: #43241

Alexey Zakhlestin a écrit :
> On Wed, Mar 4, 2009 at 2:33 PM, Richard Quadling
> <rquadling@googlemail.com> wrote:
>   
> > […]
>
> It works. You have a typo. You need to put ";" after return (…)
>
>   
Hi,

with PHP < 5.3, you can also use create_function() 
(http://www.php.net/create_function )

---

## #43244 — Richard Quadling — 2009-03-04T12:40:05+00:00

URL: https://externals.io/message/43240#43244 · In reply to: #43243

2009/3/4 Olivier B. <php-dev.list@daevel.net>:
>
> Alexey Zakhlestin a écrit :
> > […]

Just shoot me now!

> > […]
>
> Hi,
>
> with PHP < 5.3, you can also use create_function()
> (http://www.php.net/create_function )
>
> --
> 
> 
>
>



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
