Use PHP functions in JavaScript

JavaScript get_defined_functions

Returns an array of all defined functions

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
function get_defined_functions () {
    // Returns an array of all defined functions  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/get_defined_functions    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Test case 1: If get_defined_functions can find itself in the defined functions, it worked :)
    // *     example 1: function test_in_array (array, p_val) {for(var i = 0, l = array.length; i < l; i++) {if(array[i] == p_val) return true;} return false;}
    // *     example 1: funcs = get_defined_functions();    // *     example 1: found = test_in_array(funcs, 'get_defined_functions');
    // *     results 1: found == true
    var i = '', arr = [], already = {};
 
    for (i in this.window) {        try {
            if (typeof this.window[i] === 'function') {
                if (!already[i]) {
                    already[i] = 1;
                    arr.push(i);                }
            }
            else if (typeof this.window[i] === 'object') {
                for (var j in this.window[i]) {
                    if (typeof this.window[j] === 'function' && this.window[j] && !already[j]) {                        already[j] = 1;
                        arr.push(j);
                    }
                }
            }        }
        catch (e) {
            // Some objects in Firefox throw exceptions when their properties are accessed (e.g., sessionStorage)
        }
    } 
    return arr;
}
external links: original PHP docs | raw js source

Examples

Running

1
2
3
function test_in_array (array, p_val) {for(var i = 0, l = array.length; i < l; i++) {if(array[i] == p_val) return true;} return false;}
funcs = get_defined_functions();
found = test_in_array(funcs, 'get_defined_functions');

Should result in

1
found == true

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 get_defined_functions 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
Kevin van Zonneveld
30 Dec '08 Permalink

q  @ Brett Zamir: Awesome!

Gravatar
Brett Zamir
21 Dec '08 Permalink

q  And here's get_defined_vars(). As far as iterating deeper over its object properties to get a few other globals, the function turns up 'document' and 'location' which would not otherwise be present.

[CODE=&quot;Javascript&quot;]
function get_defined_vars() {

var i = '', ct = 0, obj = {};

for (i in window) {
try {
// if (typeof window[i] !== 'function') { // Uncomment if wish to ignore functions (though in JavaScript, they really are variables) // sessionStorage gives errors here in Mozilla
obj[i] = window[i];
if (typeof window[i] === 'object') {
for (var j in window[i]) { // 'history', 'globalStorage' gives errors here in Mozilla
if (window[j] !== undefined) { // window/parent/top/frames/self give errors here in Mozilla
// if (typeof window[j] !== 'function') { // Uncomment if wish to ignore functions (though in JavaScript, they really are variables)
obj[j] = window[j];
// }
}
}
}
// }
}
catch (e) {
}
ct++;
}
// window.length = ct; // Uncomment if wish to create an array-like object
return obj;
}[/CODE]

Gravatar
Brett Zamir
21 Dec '08 Permalink

q  Here's a slight improvement...For some reason, in Mozilla at least, you actually have to iterate over the window properties which are objects to get a few other globals not returned by a mere iteration of window itself (in Mozilla, these are: QueryInterface, addEventListener, getComputedStyle).

[CODE=&quot;Javascript&quot;]function get_defined_functions() {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir
// % note 1: Test case 1: If get_defined_functions can find itself in the defined functions, it worked :)
// * example 1: function test_in_array(array, p_val) {for(var i = 0, l = array.length; i &lt; l; i++) {if(array[i] == p_val) return true;} return false;}
// * example 1: funcs = get_defined_functions();
// * example 1: found = test_in_array(funcs, 'get_defined_functions');
// * results 1: found == true

var i = '', arr = [], already = {};

for (i in window) {
try {
if (typeof window[i] === 'function') {
if (!already[i]) {
already[i] = 1;
arr.push(i);
}
}
else if (typeof window[i] === 'object') {
for (var j in window[i]) {
if (typeof window[j] === 'function' &amp;&amp; window[j] &amp;&amp; !already[j]) {
already[j] = 1;
arr.push(j);
}
}
}
}
catch (e) {

}
}

return arr;
}
[/CODE]


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: @HalfWinter, Paulo Freitas, Andros Peña Romo, 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 !