# Performance question about create_function

- Thread: https://externals.io/message/45843
- Original archive: https://news-web.php.net/php.internals/45843
- Started by Mathieu Suen on 2009-10-19T12:13:39+00:00
- Messages: 4
- 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

---

## #45843 — Mathieu Suen — 2009-10-19T12:13:39+00:00

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

Hi,

Looking at the way array_map is working, it could worth to use 
create_function for object.

For example if I got a list of object:

$callback = create_function($element, 'return $selement->getId();');
$ids = array_map($callback, $someInstances);


But I don't know if it's cost a lot in performance instead of the ugly 
verbose way:

$ids = array();
foreach($someInstances)
{
	$ids[] = $someInstances->getId();
}

Of course I don't expect it to be faster but I hope that could worth the 
cost.

Thanks

-- 
-- Mathieu Suen

---

## #45876 — Michael Stillwell — 2009-10-24T13:09:51+00:00

URL: https://externals.io/message/45843#45876 · In reply to: #45843

On Mon, Oct 19, 2009 at 1:13 PM, Mathieu Suen
<mathieu.suen@easyflirt.com> wrote:

> Looking at the way array_map is working, it could worth to use
> create_function for object.
>
> For example if I got a list of object:
>
> $callback = create_function($element, 'return $selement->getId();');
> $ids = array_map($callback, $someInstances);
>
>
> But I don't know if it's cost a lot in performance instead of the ugly
> verbose way:
>
> $ids = array();
> foreach($someInstances)
> {
>        $ids[] = $someInstances->getId();
> }

I think what you want to do can be done with anonymous functions:

    $arr1 = array(
        (object) array("id" => 56),
        (object) array("id" => 42)
    );

    $ids = array_map(function($obj) { return $obj->id; }, $arr1);

    print_r($ids);

produces:

    Array
    (
        [0] => 56
        [1] => 42
    )



Michael

-- 
http://beebo.org
+44 78 2118 9049

---

## #45882 — troels knak-nielsen — 2009-10-26T14:31:56+00:00

URL: https://externals.io/message/45843#45882

On Mon, Oct 26, 2009 at 3:25 PM, Mathieu Suen
<mathieu.suen@easyflirt.com> wrote:
> Yes that the same but only for PHP <= 5.3.
> I am more asking about performance.

You mean PHP < 5.3 ?

In that case, I would strongly suggest that you use a procedural
style. create_function is slow, leaks memory and offers no
compile-time validation of syntax. It's also ugly as heck.

-- 
troels

---

## #45894 — Karsten Dambekalns — 2009-10-29T09:29:58+00:00

URL: https://externals.io/message/45843#45894

Hi.


> I am more asking about performance.

I would have been ready to sacrifice performance if it saved money. But
according to my quick tests, it does not. See http://tr.im/DrsH for
details... oh, this is on PHP 5.3.

Any other insights on this welcome!

Regards,
Karsten

PS: This is off-topic for php-internals, probably...
