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

2
  • 1
    Why don't you like the current solution? Commented Feb 9, 2012 at 18:08
  • 2
    Consider the security implications if your example were true.. Anyone could call arbitrary class functions from the browser! Commented Feb 9, 2012 at 18:10

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

See Remote Procedure Call - en.wikipedia.org/wiki/Remote_procedure_call (RPC), for example JSON-RPC - en.wikipedia.org/wiki/JSON-RPC
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;

Comments

1

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

Comments

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/

Comments

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']);

1 Comment

You could improve this answer by adding comments explaining how your code works to answer the question...
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.