Use PHP functions in JavaScript

JavaScript create_function

Creates an anonymous function, and returns its name (funny, eh?)

1
2
3
4
56
7
8
9
1011
12
13
14
1516
function create_function (args, code) {
    // Creates an anonymous function, and returns its name (funny, eh?)  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/create_function    // +   original by: Johnny Mast (http://www.phpvrouwen.nl)
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: f = create_function('a, b', "return (a + b);");
    // *     example 1: f(1, 2);
    // *     returns 1: 3    try {
        return Function.apply(null, args.split(',').concat(code));
    } catch (e) {
        return false;
    }}
external links: original PHP docs | raw js source

Examples

Running

1
2
f = create_function('a, b', "return (a + b);");
f(1, 2);

Should return

1
3

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have create_function goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Seingh
1 Feb '11 Permalink

q  I think that a "different version"of the code would be:

function create_function (args, code) {
args = args.split(",");
return eval(code);
}

Gravatar
Brett Zamir
28 Apr '10 Permalink

q  @Diplom@t : thanks very much for the input. Applied in Git to call_user_func() and call_user_func_array(). See also comment at call_user_func_array.

Gravatar
Diplom@t
28 Apr '10 Permalink

q  "You can still handle the functions in the same way as PHP:
[CODE="Javascript"]
call_user_func_array(create_function(), $a);
[/CODE]"

Acutally you can't. I've tried the following code:

var func = create_function("x", "return x*x;");
var res = call_user_func(func, 5);


Error is being thrown:
"Error: undefined is not a valid function"

But if you add the third else like the one below, everything works:

else if (typeof cb == 'function')
{
    func = cb;
}

Gravatar
Kevin van Zonneveld
8 Sep '08 Permalink

q  @ cedric: Hi Cedric, that's a very sharp observations. We have chosen to astray a little bit from PHP's implementation here.

You can still handle the functions in the same way as PHP:
[CODE="Javascript"]
call_user_func_array(create_function(), $a);
[/CODE]

Only now both methods pass actual functions instead of function names (what PHP does).

It was a trade-off, because otherwise we would have to work with global variables which would be nasty.

Documentation is currently copied 1-on-1 from the PHP site, to be 100% correct we would have to make exceptions in the documentation, but unfortunately that not our number one priority at the moment.

Thanks for your input.

Gravatar
cedric
29 Aug '08 Permalink

q  The doc states that this function "Returns a unique function name as a string," like the php version would, the example code looks like it's returning the actual function, not the name of the function (in PHP, it would have named the function something like 'lambda1' and returned it).

If your other functions like array_map() expect a function instead of the function's name, it should be okay - but the documentation would still be wrong.

Also, the example is incorrect or, not clear and I'm not understanding it.


Contribute a New function

More functions

In this category

call_user_func
call_user_func_array
» create_function
forward_static_call
forward_static_call_array
func_get_arg
func_get_args
func_num_args
function_exists
get_defined_functions
register_shutdown_function

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !