1 (edited by karman 2012-03-04 12:03:24 AM)

Topic: problem using xajax in a object oriented environment

Hi everybody,
I am using xajax since a while and I'm finding it very user-friendly.

I stumbled on a problematic issue, maybe I'm trying to make a weird use of the library
or maybe I'm just missing/mistaking some obvious thing which someone else will be so
kind to advise me about.

Here is the matter:

I built up a class in which there are two methods that are registered as xajax functions
so I registered them with the object oriented environment syntax:

$this->xajax->register(XAJAX_FUNCTION, array('myFunNumOne', $this, 'myFunNumOne') );  
$this->xajax->register(XAJAX_FUNCTION, array('myFunNumTwo', $this, 'myFunNumTwo') );  

The point is that I need (or by now I think I need) let's say myFunNumTwo to use
a class attribute $this->myattrib that it's being previously updated by myFunNumOne
and this last function formats the client code to invoke myFunNumTwo upon a particular
user interface event.

Now I've found out this: when I print out the actual value of $this->myattrib within
myFunNumTwo I realize it has remained at the initial value assigned by the class constructor
upon object initialization even if the previously invoked myFunNumOne has changed it's value.

It seems that, wihtin a function, the object istance can be normally updated in all its attributes
and this is of course desiderable but as the call stack passes to another function the attributes
are resetted to initial values and the only explaination I could give myself it's that this is due
to function registration with the "mint" object istance:

$this->xajax->register(XAJAX_FUNCTION, array('myFunNumOne',  $this, 'myFunNumOne') );  
$this->xajax->register(XAJAX_FUNCTION, array('myFunNumTwo',  $this, 'myFunNumTwo') );  

So 1) I tried to pass that istance by reference with no result:

$this->xajax->register(XAJAX_FUNCTION, array('myFunNumOne',  &$this, 'myFunNumOne') );  
$this->xajax->register(XAJAX_FUNCTION, array('myFunNumTwo',  &$this, 'myFunNumTwo') );  

then 2) I tried to share the same $objResponse within all the xajax registered functions by
declaring it at class scope and not at method scope, and no success also with this:

class myClass  

protected $objResponse;


public function __construct()  

$this->$objResponse = new xajaxResponse;




finally 3) I even tried to pass the serialized class istance as a myFunNumTwo parameter
but first of all this looks like a very dirty workaround and then it's almost impossible to let
it work if "by chance" there are rich formatted data inside the istance attributes. This doesn't
sound as a great solution neither if it is passed just the specific class attribute cause it's
just a negation of the object oriented paradigma. (sic)

Any idea on how to invoke a xajax function within another xajax function letting them share the
same object istance at the actual values the attributes got thru the previous "caller-function"

I hope I could explain cleary... many thanks in advance to any contributor!
Keep up the good work!

Re: problem using xajax in a object oriented environment


I think I see wht you are getting at.

1. Browser calls myFunNumOne and it alters the attribute.

2. Browser calls myFunNumTwo and it does not see the altered attribute.

This is because every call to the server causes the page to be reloaded and initialized. There is no "memory" of the previous call's actions.

I would probably store the value in a session variable thus making it available to the next call.

Hope this helps... smile


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

3 (edited by karman 2012-03-04 7:20:22 PM)

Re: problem using xajax in a object oriented environment

Hi Ed,
thanks for your contribute!

I thought the point with ajax was that client doesn't reload
upon a registered function call.

Take in account that in my registered function (let's say n.1)
I format in the client redundant calls of itself with different actual parameters
and in such cases, upon a client event, the object istance is never resetted
and those are in any case further function calls.

I think there must be yet something tricky that I'm missing but
I'll take in consideration the use of session variables, it's
for sure better than serialized stuff passed as parameter... smile

The point is that I'm building a self-sufficient object that has to
"live" by itself and do what it's supposed to do with the less interaction
with the module that makes use of it.

Thank you so much for your feedback, see you!

Re: problem using xajax in a object oriented environment

That is the idea - the client does not reload with each server call.

Server side, however, your php page is reloaded at each call and your call is intercepted by the xajax processRequest function. It calls the function specified in the inconing XHR request and returns the function's response to the client.

Thus, if you need to "remember" anything between server calls, the server must save it in session variables or return it to hidden inputs that the client sends back each time it calls.


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