tags:

views:

182

answers:

2

I'm a fairly experienced programmer getting my head around PHP and Ajax for the first time, and I'm having a bit of trouble figuring out how to incorperate object oriented PHP into my ajax webapp.

I have an admin page (admin.php) that will load and write information (info.xml) from an XML file depending on the users selection of a form on the admin page. I have decided to use an object (ContentManager.php) to manage the loading and writing of the XML file to disk, i.e :

class ContentManager{

 var $xml_attribute_1
 ...

 function __construct(){
    //load the xml file from disk and save its contents into variables
    $xml_attribute = simplexml_load_file(/path/to/xml)
 }
 function get_xml_contents(){
    return xml_attribute;
 }
 function write_xml($contents_{
 }
 function print_xml(){
 }    
}

I create the ContentManager object in admin.php like so

<?php
include '../includes/CompetitionManager.php';
$cm = new CompetitionManager()
?>
<script>
  ...all my jquery
</script>
<html>
  ... all my form elements
</html>

So now I want to use AJAX to allow the user to retrieve information from the XML file via the ContentManger app using an interface (ajax_handler.php) like so

<?php
  if(_POST[]=="get_a"){

  }else if()
  }
  ...
 ?>

I understand how this would work if I wasn't using objects, i.e. the hander php file would do a certain action depending on a variable in the .post request, but with my setup, I can't see how I can get a reference to the ContentManager object I have created in admin.php in the ajax_handler.php file? Maybe my understanding of php object scope is flawed.

Anyway, if anyone can make sense of what I'm trying to do, I would appreciate some help!

+1  A: 

think of each ajax call as a separate request. if in the life cycle of a particular request you have not instantiated your ContentManager, the object doesn't exist. If you'd like to use a single object between multiple requests, serialize it to session and deserialize it early in the request life cycle.

Rich
well I've instantiated the ContentManager object at the start of the admin.php file so it should still exist throughout the users interactions with the form?
pastylegs
nah...if it's an ajax call, it's an entirely separate http request. the fact that the browser is not "posting back" can cause some confusion, but an ajax request follows the same life cycle as a page request or a form post back. if this is a little tough to visualize, download the Live HttpHeaders plugin for Firefox and watch the ajax requests go from client to server on your page.
Rich
hmmm, maybe my understanding of this is a little off. I understand that when you do an Ajax request, it's an entirely new http call and so has a short lifespan, but if admin.php loads up, and I create the php object then, does that php object not reside on the server indefinitely until the reference is lost, i.e. until the user navigates away from the page? This would mean that any Ajax request in the meantime would be able to access that object
pastylegs
no. think of your server and the client as completely disconnected. when the page is done rendering on the server, it is sent to the client and everything is destroyed. in order for anything to live in between requests, you have to use some kind of in-memory or file storage and pull the object out of it in subsequent requests. since you're working in php, that would be the $_SESSION
Rich
ah I get you, I thought objects stayed alive on the server. It seems that objects in PHP are a bit of hassle and it would be easier just to do it procedurally? Is it normal practice and safe to encode the objects in session variables?
pastylegs
yeah...architect it to make it work for you. the whole http protocol was built before server side languages like php, so php is the square peg that as a programmer you have to fit in the round hole of http. When I did a lot of php, I'd cache a decent amount of stuff in session to save db round trips and expensive initializations, and there's a lot of good, clean ways to do it. asp.net was even more interesting in this sense because in addition to session memory (unique per user) there is an application memory space (shared by all users) and machine cache (shared by all apps).
Rich
yea I like the idea of saving the objects in session, so I'll try that out. It makes sense as I will be using sessions anyway to track logins. Thanks again
pastylegs
A: 

I dont know if this is what you need, well, here goes. Have a single PHP file to handle all the form submissions. For eg: proc.php or something like that in the ACTION="proc.php". Inside the proc.php, depending upon the parameters submitted, make function calls. One other thing you should likely do is to create an instance of the class (the object) at the end of the class file itself avoiding the need to check everytime if the object was instantiated or not. Use global $objectname before you make calls to the object functions, if necessary.

Joey Ezekiel
I've actually done that in ajax_handler.php. The problem is that the ContentManager php class is a separate file and is instantiated separately to ajax_handler.php
pastylegs
Just include the contentmanager file in the ajax_handler.php instead of the admin.php and use the object. Is that the case or am I misunderstanding something here?
Joey Ezekiel