Does anyone know how I can directly access a function from a PHP class through AJAX (using jQuery).
PHP:
class Blah
{
function __construct()
{
}
function doSomething(data)
{
echo "I am not an animal";
}
}
jQuery:
$.ajax({
url: myClass.php,
data: Blah.doSomething(); "or" Blah->doSomething() "or whatever"
});
I know this is a crude example, i’m just trying to illustrate a point, I hope you can get the gist of my question.
At the moment i’m doing something along these lines:
$.ajax({
url: myClass.php,
data: data : { 'type':'doSomething' }
});
||
if(POST['data']['type'] == 'doSomething')
{
$this->doSomething();
}
And I don’t like it…
You need to create an object of that class and then invoke the methods you need.
The class declaration doesn’t not execute any code.
For example, you can add this below the class:
UPDATE:
If you want to invoke the method sent in POST, you can use the function call_user_function:
See here more details: http://php.net/manual/en/function.call-user-func.php