Use PHP functions in JavaScript

JavaScript runkit_class_adopt

!No description available for runkit_class_adopt. @php.js developers: Please update the function summary text file.

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
39
4041
42
43
44
45
function runkit_class_adopt (classname, parentname) {
    // !No description available for runkit_class_adopt. @php.js developers: Please update the function summary text file.
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/runkit_class_adopt    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: Function can only obtain and set classes from the global context
    // *     example 1: function A () {}
    // *     example 1: A.prototype.methodA = function () {};
    // *     example 1: function B () {}    // *     example 1: runkit_class_adopt('B', 'A');
    // *     returns 1: true
 
    if (typeof this.window[classname] !== 'function' || typeof this.window[parentname] !== 'function') {
        return false;    }
 
    // Classical style of inheritance
    this.window[classname].prototype = new this.window[parentname](); // Has side effects by calling the constructor!
 /*
    // Prototypal completely by reference
    this.window[classname].prototype = parentname.prototype; // By mutual reference!
*/
 /*
    // Mixin (deep copy, not by reference)
    var _copy = function (child, parent) {
        var p = '';
        for (p in parent) {            if (typeof parent[p] === 'object') {
                child[p] = _copy(child[p], parent[p]);
            }
            else {
                child[p] = parent[p];            }
        }
    };
    _copy(this.window[classname].prototype, this.window[parentname].prototype);
*/ 
    // Put original constructor property back
    this.window[classname].constructor = this.window[classname];
    return true;
}
external links: original PHP docs | raw js source

Examples

Running

1
2
3
4
function A () {}
A.prototype.methodA = function () {};
function B () {}
runkit_class_adopt('B', 'A');

Should return

1
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 runkit_class_adopt 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

No comments yet. Be the first!


Contribute a New function