Take the 2-minute tour ×

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 incorporate 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

include '../includes/CompetitionManager.php';
$cm = new CompetitionManager()
  ...all my jquery
  ... all my form elements

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


  }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!

share|improve this question
add comment

2 Answers

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.

share|improve this answer
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 Mar 30 '10 at 18:59
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 Mar 30 '10 at 19:02
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 Mar 30 '10 at 19:15
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 Mar 30 '10 at 19:22
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 Mar 30 '10 at 19:54
show 2 more comments

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.

share|improve this answer
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 Mar 30 '10 at 19:16
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 Mar 30 '10 at 19:26
add comment

Your Answer


