phpjs
Version:
69 lines (59 loc) • 2.33 kB
Markdown
---
layout: page
title: "JavaScript runkit_method_rename function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/runkit_method_rename:817
- /functions/view/runkit_method_rename
- /functions/view/817
- /functions/runkit_method_rename:817
- /functions/817
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's runkit_method_rename
{% codeblock runkit/runkit_method_rename.js lang:js https://raw.github.com/kvz/phpjs/master/functions/runkit/runkit_method_rename.js raw on github %}
function runkit_method_rename (classname, methodname, newname) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// * example 1: runkit_method_rename('someClass', 'someMethod', 'newMethod');
// * returns 1: true
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
};
if (typeof classname === 'string') {
classname = this.window[classname];
}
/*
var method = classname[methodname]; // Static
classname[newname] = method;
delete classname[methodname];
*/
if (getFuncName(classname) !== 'PHP_JS' || // By default, don't allow overriding of PHP functions
(this.php_js && this.php_js.ini && this.php_js.ini['runkit.internal_override'] && (this.php_js.ini['runkit.internal_override'].local_value === true || this.php_js.ini['runkit.internal_override'].local_value === 1 || this.php_js.ini['runkit.internal_override'].local_value === '1' || this.php_js.ini['runkit.internal_override'].local_value === 'true'))) {
var method = classname.prototype[methodname];
classname.prototype[newname] = method;
delete classname.prototype[methodname];
return true;
}
return false;
}
{% endcodeblock %}
- [view on github](https://github.com/kvz/phpjs/blob/master/functions/runkit/runkit_method_rename.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/runkit/runkit_method_rename.js)
### Example 1
This code
{% codeblock lang:js example %}
runkit_method_rename('someClass', 'someMethod', 'newMethod');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Other PHP functions in the runkit extension
{% render_partial _includes/custom/runkit.html %}