2

i am newbie in Jquery and Ajax.. Please bear with my stupid problem..

i am trying to call method say test() inside class hello through ajax call..

hello.php

class hello
{
      public function test()
      {
        //some data
      }

      public function abc()
      {
        //some data
      }
}

now i want to call test() from another php file...

for example:

b.php

  $.ajax({
    url : 'hello.php->test()', //just for example i have written it bcz it should call only test() not abc()..
   })

is it possible to call it directly? i have went through $.ajax() api but i din't found anything helpful..

All answer will be appreciates...

9
  • AJAX just calls a single web address (so a PHP file, in your case). If it's possible to declare a class in one PHP file (say a.php) and then access it in another PHP file (b.php), then yes this can be done - but the AJAX call won't be any different, you'll be calling b.php, the JavaScript doesn't care what that PHP file does provided it returns a usable response. Commented Sep 27, 2013 at 10:57
  • You are not directly calling any PHP methods via AJAX. AJAX is just a way of making a request to the Server dynamically. This request could be handled any way you want. Commented Sep 27, 2013 at 10:57
  • you cann't call it from outside as it is private to hello Commented Sep 27, 2013 at 10:57
  • @ArunPJohny it's a public function - all you have to do is instantiate hello then it's visible. Commented Sep 27, 2013 at 11:00
  • put the class in a separated file. You'll have:hello.class.php, a.php, b.php. With that, you can freely call Hello->test() in a.php or b.php Commented Sep 27, 2013 at 11:00

2 Answers 2

1

Try this:

hello.php

class hello
{
      public function test()
      {
        //some data
      }

      public function abc()
      {
        //some data
      }
}
if(isset($_GET['method'])){
   $hello = new hello;
   $hello->$_GET['method']();
}

b.php

 $.ajax({
    url : 'hello.php?method=test', //just for example i have written it bcz it should call only test() not abc()..
   })

By they way, it's not secure to expose your class via ajax requests.

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

Comments

1

One approach is to pass the name of the class, the constructor arguments, and the method name and arguments etc. via ajax POST or GET, for example:

var url = 'callMethod.php';
var data = {
    str_className: 'Hello',
    arr_consArgs: {arg1: 'test1'},
    str_methodName: 'test'
};
$.post(url, data, function(response) {
    etc.
});

In PHP script named callMethod.php:

/* Place your 'Hello' class here */

// class
$str_className = !empty($_POST["str_className"]) ? $_POST["str_className"] : NULL;
if ($str_className) {
    // constructor
    $arr_consArgs = !empty($_POST["arr_consArgs"]) ? $_POST["arr_consArgs"] : array();

    // method
    $str_methodName = !empty($_POST["str_methodName"]) ? $_POST["str_methodName"] : NULL;
    if (!empty($str_methodName)) {
        $arr_methodArgs = !empty($_POST["arr_methodArgs"]) ? $_POST["arr_methodArgs"] : array();
    }

    // call constructor
    $obj = fuNew($str_className, $arr_consArgs);

    // call method
    $output = NULL;
    if (!empty($str_methodName)) 
        $output .= call_user_func_array(array($obj, $str_methodName), $arr_methodArgs);

    // echo output
    echo $output;

}

where:

function fuNew($classNameOrObj, $arr_constructionParams = array()) {
    $class = new ReflectionClass($classNameOrObj);
    if (empty($arr_constructionParams))
        return $class->newInstance();
    return $class->newInstanceArgs($arr_constructionParams);
}

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.