PHP's call_user_func_array in JavaScript

How to use

You you can install via yarn add locutus and require this function via const call_user_func_array = require('locutus/php/funchand/call_user_func_array').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1call_user_func_array('isNaN', ['a'])true
2call_user_func_array('isNaN', [1])false

Notes

  • Depending on the cb that is passed, this function can use eval and/or new Function. The eval input is however checked to only allow valid function names, So it should not be unsafer than uses without eval (seeing as you can) already pass any function to be executed here.

Here’s what our current JavaScript equivalent to PHP's call_user_func_array looks like.

module.exports = function call_user_func_array(cb, parameters) {
// discuss at: https://locutus.io/php/call_user_func_array/
// original by: Thiago Mata (https://thiagomata.blog.com)
// revised by: Jon Hohle
// improved by: Brett Zamir (https://brett-zamir.me)
// improved by: Diplom@t (https://difane.com/)
// improved by: Brett Zamir (https://brett-zamir.me)
// note 1: Depending on the `cb` that is passed,
// note 1: this function can use `eval` and/or `new Function`.
// note 1: The `eval` input is however checked to only allow valid function names,
// note 1: So it should not be unsafer than uses without eval (seeing as you can)
// note 1: already pass any function to be executed here.
// example 1: call_user_func_array('isNaN', ['a'])
// returns 1: true
// example 2: call_user_func_array('isNaN', [1])
// returns 2: false

const $global = typeof window !== 'undefined' ? window : global
let func
let scope = null

const validJSFunctionNamePattern = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/

if (typeof cb === 'string') {
if (typeof $global[cb] === 'function') {
func = $global[cb]
} else if (cb.match(validJSFunctionNamePattern)) {
func = new Function(null, 'return ' + cb)() // eslint-disable-line no-new-func
}
} else if (Object.prototype.toString.call(cb) === '[object Array]') {
if (typeof cb[0] === 'string') {
if (cb[0].match(validJSFunctionNamePattern)) {
func = eval(cb[0] + "['" + cb[1] + "']") // eslint-disable-line no-eval
}
} else {
func = cb[0][cb[1]]
}

if (typeof cb[0] === 'string') {
if (typeof $global[cb[0]] === 'function') {
scope = $global[cb[0]]
} else if (cb[0].match(validJSFunctionNamePattern)) {
scope = eval(cb[0]) // eslint-disable-line no-eval
}
} else if (typeof cb[0] === 'object') {
scope = cb[0]
}
} else if (typeof cb === 'function') {
func = cb
}

if (typeof func !== 'function') {
throw new Error(func + ' is not a valid function')
}

return func.apply(scope, parameters)
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP funchand functions


Star