UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

85 lines (64 loc) 2.69 kB
<!-- Generated by Rakefile:build --> <strong> <a href="patronum.tk" rel="nofollow">Seingh</a> </strong> on 2011-02-01 20:48:39 <br /> I think that a &quot;different version&quot;of the code would be: <pre><code> function create_function (args, code) { args = args.split(&quot;,&quot;); return eval(code); } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-04-28 18:42:14 <br /> @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. <hr /> <strong> <a href="difane.com" rel="nofollow">Diplom@t</a> </strong> on 2010-04-28 15:19:49 <br /> &quot;You can still handle the functions in the same way as PHP: <pre><code> call_user_func_array(create_function(), $a); </code></pre>&quot; Acutally you can't. I've tried the following code: <pre><code> var func = create_function(&quot;x&quot;, &quot;return x*x;&quot;); var res = call_user_func(func, 5); </code></pre> Error is being thrown: &quot;Error: undefined is not a valid function&quot; But if you add the third else like the one below, everything works: <pre><code> else if (typeof cb == 'function') { func = cb; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-09-08 00:14:51 <br /> @ 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: <pre><code> call_user_func_array(create_function(), $a); </code></pre> 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. <hr /> <strong> cedric </strong> on 2008-08-29 09:00:00 <br /> The doc states that this function &amp;quot;Returns a unique function name as a string,&amp;quot; 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. <hr />