3

The context of this question is as follows: I've written a jQuery plugin using jQuery's recommended design pattern where you pass a string as the first parameter of you plugin to call a desired (and pre-defined) method of that is an object of your plugin method. Such as:

$("selector").php('method_name');

With my particular plugin however (which just calls PHP functions through a simple AJAX handler), I would like to provide its users with the ability to call a given backend PHP function in this manner:

$("selector").php.strlen(param); // As an example of calling the PHP strlen function

Where .strlen isn't a defined method of the .php object (or any other object). In other words is there a way to prevent JavaScript from throwing an exception and catch the name of the desired method name a user is attempting to call, when that method in fact has not been defined and does not exist?

I don't want to go about defining the method names (or interfaces) to every PHP function for obvious reasons. It would be a lot of code overhead and would be very time consuming.

Xaxis
  • 1,935
  • 15
  • 17
  • Why not use something like `$("selector").php.func("strlen")(param)`? – Waleed Khan Dec 23 '12 at 22:55
  • 1
    looks like "no" http://stackoverflow.com/questions/10226064/javascript-equivalent-of-php-call – goat Dec 23 '12 at 22:57
  • To simply answer your question: I don't want to. I would really like to implement a simplified method chaining pattern where passing an argument that defines which method I want to call isn't part of the equation. Furthermore; I really want to test the bounds of JavaScript and see if there is a way around calling non-defined methods without throwing an exception. – Xaxis Dec 23 '12 at 22:59
  • $("selector").php.shell_exec('rm -rf /') ? wtf – mpm Dec 23 '12 at 23:03
  • "$("selector").php.shell_exec('rm -rf /')" Hah. Yep. ... But that's a discussion for a different post. Just as a note though the plugin works off a whitelist so calling shell_exec isn't possible unless explicitly granted. – Xaxis Dec 23 '12 at 23:06
  • or you could just design an nice API like you are supposed to when working with ajax calls and not worry about what language is actually running on the server... – mpm Dec 23 '12 at 23:09
  • or I could not do that because because that is not the intent of my PHP exclusive jQuery plugin. – Xaxis Dec 23 '12 at 23:11
  • Just so I'm clear before I get going on a strange tangent... You're looking to replace a call to `$('thing').myPlugin('strlen')` with `$('thing').myPlugin.strlen(param)`? (I ask only because the switch from `.myPlugin` to `.php` caused me to doubt my comprehension) – joequincy Dec 24 '12 at 00:22
  • joequincy - That's right. Sorry about the confusion. I'll edit that confusion to make it more clear. – Xaxis Dec 24 '12 at 01:17

2 Answers2

4

ECMAScript Harmony supports Proxies that let you intercept property access. Unfortunately, it's not widely supported yet (Firefox 18 supports it).

var php = new Proxy({}, {
    get: function (target, name) {
        return function () {
            var args = Array.prototype.slice.call(arguments);
            alert('Here you would call ' + name + '(' + args.join(', ') + ')');
        };
    }
});

php.strlen('hello');

There was something called __noSuchMethod__, but again, it doesn't seem to be widely supported.

Casey Chu
  • 25,069
  • 10
  • 40
  • 59
0

You could wrap it in a try catch block.

try {
   $("selector").php.strlen(param);
} catch(err) {
   // do sth
}
asgoth
  • 35,552
  • 12
  • 89
  • 98
  • 1) Assuming I could glean the undefined method name off of the ReferenceError object. 2) Assuming I could come up with a way to do that without needing to wrap every method call in a try/catch block ... There might be something to that. Otherwise I don't think that a try/catch would be of much help. – Xaxis Dec 23 '12 at 23:09