0

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...

outrunthewolf
  • 373
  • 3
  • 13

6 Answers6

5

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:

class Blah { ... }
$b = new Blah();
$b->doSomething();

UPDATE:

If you want to invoke the method sent in POST, you can use the function call_user_function:

$method_name = $_POST['method'];
$b->{$method_name}();

See here more details: http://php.net/manual/en/function.call-user-func.php

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
  • See *Remote Procedure Call* - http://en.wikipedia.org/wiki/Remote_procedure_call (RPC), for example **JSON-RPC** - http://en.wikipedia.org/wiki/JSON-RPC – hakre Feb 09 '12 at 18:22
3

i don't know why do you like to do so. its not a good programming practice. but you can try something like this

$.ajax({
    url: myClass.php,
    data: {'call':'Blah.doSomething', 'args':5}
});

and in server side you can do like

$fun = explode('.', $call);
$function = array($fun[0], $fun[1]);
if (is_callable($function)) {
    $response = call_user_func_array($function, $args);
}
echo $response;
rajesh
  • 2,354
  • 1
  • 12
  • 15
1
**Your class**
class Blah {
    function doSomething(data) {
        echo "I am not an animal, I'm a ".data;
    }
}

**Ajax call in other document**
$.ajax({
   url: example.php,
   data: info='beast'
   success: data () { ...
});

**in example.php**
include (class.blah.php);
$obj = new SomeClass();
$obj ->doStuff($_GET['info']);
  • You could improve this answer by adding comments explaining how your code works to answer the question... – Craig Dec 13 '12 at 00:28
1

Right now I can't think of any better way than PHP outputting results in json and you get the result via getJSON

James Lin
  • 25,028
  • 36
  • 133
  • 233
1

I concur with mazzucci's answer. I hope this is a bit more complete.

PHP:

class SomeClass{ 
 //definintion 
}
$obj = new SomeClass();
$obj->doStuff();

HTML:

<input type="button" onclick="<?php $obj->doStuff(); ?>" id="btnAdd" value="Click Me" />

Also, you can look into xajax, a nice little framework that simplifies php<->javascript interaction. It can call js functions from php as well. Here's the link: http://www.xajax-project.org/en/home/

Greg Kramida
  • 4,064
  • 5
  • 30
  • 46
0

It's not possible. What you currently have is probably the best way.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308