Topic: Problem with registering functions in include files

I have this situation:
I use friendly URLs without patch .htacess file. So i have an index.php that handle all te routes. Something like this:

<?php
require_once("lib/xajax/xajax_core/xajax.inc.php");
$_GET["xajax"] = new xajax();

//all the conditionals for load the pages
include("pages/any_file.php");
?>

The file any_file.php it's like this:

<?php
function test(){
  $resp = new xajaxResponse();
  $resp -> alert("works");
  $return $resp;
}
$_GET["xajax"] -> register(XAJAX_FUNCTION, "test");

include("templates/header.php");
include("templates/body.php");
include("templates/footer.php");
?>

Inside templates/header.php i have:

<?php
$_GET["xajax"] -> processRequest();
?>
<html>
<head>
<?php $_GET["xajax"] -> printJavascript("lib/xajax"); ?>
</head>

How we can see, the xajax object is instancied at the index.php (the main file), the function is created and registered at the any_page.php (included by index.php) and the javascript print is located at templates/header.php (included by any_page.php).

It's kind of confused, but i just face the problem in can't create 2 xajax objects now, when my projects it's almost done. Until now, everything works fine and all the xajax tasks (instance, register functions and print javascript) where done in a single file. But now i need handle xajax in global level in my project, what explain all this mess.

Keep going with the issue, the function test() it's sucessfully registered. I can call her normally, but i get an "false" as return. I think that is because the function it's been registered inside an include file.

Any ideas?
Sorry for the bad writing. I'm brazilian.

Re: Problem with registering functions in include files

I see that this question was asked some time ago, but I want to answer it so others encountering this issue can resolve the issue more readily.

The only way for xajax to handle a request (currently) is to have that request registered when the page is first loaded.  I call this IPL (Initial Page Load).  So, you could set a flag indicating when IPL is occurring, then adjust your include files to act accordingly.

$_GET['IPL'] = true;

In each include file:

<?php
function test(){
  $resp = new xajaxResponse();
  $resp -> alert("works");
  $return $resp;
}
$_GET["xajax"] -> register(XAJAX_FUNCTION, "test");

if (isset($_GET['IPL']) == false) {
include("templates/header.php");
include("templates/body.php");
include("templates/footer.php");
}
?>

Then, it is necessary for you to call all of the include files that contain xajax registration during IPL.  This must be done before processRequest is called.  Xajax needs to know about each registration in order to generate the browser side javascript to initiate an ajax request.

I hope this helps.

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct