1 (edited by edrobinson 2011-07-31 3:00:57 AM)

Topic: Register only one function per page

Hi All,

I haven't seen any mention of this technique using PHP 5.x with xajax.

With php 5 you can use a variable as a function name.

...
$myvar(); //$myvar contains the function name and this syntax calls it.

...

I have always espoused using a "dispatcher to call the functions in an xajax page  but it has aways had a big if/else or switch statement in it.

Now it can look like this:

//The called function
function myfunc($data){
  $resp = new xajaxResponse();
  ... //Do something 
  return $resp;
}

//The dispatch function
function dispatcher($op, $data){
  return $op($data); //if $op = myfunc this calls myfunc() passing in $data as the parameter
}
$xajax->register(XAJAX_FUNCTION,'dispatcher');

Now I can do a simple js in the client <head> to make client side simpler too:

  //Call the dispatcher passing the form data
  function sendPage($op){
    xajax_dispatcher(xajax.getFormValues('form1'));
 }
...

<input type="button onclick="sendPage('myfunc)"/>

Works well. Anyone see any issues?

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.