Jump to content

php classes with ajax


jonniejoejonson

Recommended Posts

I am trying to create a page that uses both ajax and classes.

The classes are defined on the main page php page.

The ajax request is sent to an external php script.

My problem is that the external script does not have access to the classes and objects  defined in the main php script... this means, top use a class i have to redefine the class in the external script... which also means i can't intereact with the objects cerated in the main script...

Is there anyway to do this... or do you simply not use classes with ajax?...

kind regards... J.

Link to comment
Share on other sites

Hi

 

When the AJAX script is run the main script has finished and everything has been discarded so there are no classes to use. If you want them then you would have to recreate them in the AJAX script.

 

What you can try is to store the class in a session variable, so your AJAX script just does a sesion_start() and then . This is meant to work but I am a bit dubious about storing large amounts of data in session variables.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

What you can try is to store the class in a session variable, so your AJAX script just does a sesion_start() and then . This is meant to work but I am a bit dubious about storing large amounts of data in session variables.

 

There is an example of storing an object in a variable and then extacting it again on the serialize function page on the php site:-

 

http://us.php.net/manual/en/function.serialize.php

 

All the best

 

Keith

Link to comment
Share on other sites

Firstly thanks for all your responses...

So the answer is as i understand it...

i can use ajax to effect the objects created by classes but first i must make SESSIONS of the objects, so that they are accessable to the external php scripts called by ajax...

 

This leads me to my next question:...

this deos not work...

 

$user_object=new userClass();

$_SESSION['user_object']=$user_object;

 

I can not access the SESSION objects attributes...

however it does works if i use:

 

$_SESSION['user_object']=new userClass();

 

... can anyone explain why?

regards J

Link to comment
Share on other sites

Hi

 

Couple of things. You need to serialise the objects before saving them in a session variable, and unserialise them to use them.

 

Also the declares for the classes need to go before the session_start it seems .

 

Not tried it so I will go and have a play (not sure how much data is not going to be changed by other users between pages and so which would be suitable for storing in an object).

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Quick play and this appears to work.

 

<?php

class SomeClass 
{

private $SomeData;

function __construct() 
{
	$SomeData = 0;
}

function setSomeData($InField)
{
	$RetField = false;
	if (is_numeric($InField))
	{
		$this->SomeData = $InField;
		$RetField = true;
	}
	return $RetField;
}
function getSomeData()
{
	return $this->SomeData;
}

}

session_start();

if (isset($_SESSION['SomeClass']))
{
echo "Existing Class";
$SomeClass = unserialize($_SESSION['SomeClass']);
}
else
{
echo "New Class";
$SomeClass = new SomeClass();
}

$SomeClass->setSomeData($SomeClass->getSomeData() + 5);

echo "*".$SomeClass->getSomeData()."*<br />";

$_SESSION['SomeClass'] = serialize($SomeClass);

?>

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

I would presume if you want to store some data that was fairly complex to calculate, and you want to avoid recalculating it.

 

Eg, something like an insurance price comparison site where you can use Ajax to adjust the excess with a slider. If you have one object stored for each quote with all the details already stored then you can easily just pass the new excess value and trigger each one to recalculate and reproduce the quote.

 

All the best

 

Keith

Link to comment
Share on other sites

However if for example you want to change a user name, then simply sending the id and a new name would be enough instead of cramming a User object inside a session.

 

Very true. I struggle to think of anything in the systems I code in that could benefit from it. Only cases are some large paging setups where you might want to alter the order of the data, and where the data comes from multiple seperate databases (which I already have to read in and then sort in a multidimensioned array to find what I need for a particular page). Even then I am dubious as some of the data could be altered by another user between creating the object and the next time you access it.

 

All the best

 

Keith

Link to comment
Share on other sites

Thanks keith the serialize and unserialize solves the problem i was having....

 

With regard to what i am trying to do:

 

I have a user that is trying to update a forum posting via ajax.

 

The main script creates a userObject that stores the userId and user details.

 

Rather than send the external php script that ajax calls the userId, I am storing the user_object in a session... it is then available to the exernal php script that ajax calls...

 

Is this the right way to go about it?..

regards J. 

Link to comment
Share on other sites

Hi

 

Possibly. Partly depends how complex he details are. Very complex and it is worthwhile, but then loads of users logged on and you will land up with a massive amount of memory used by these objects. Small simple data and not sure you need an object in the first place, just retrieve it from the table each time a screen is returned (how phpBB does it), and just store the userid in a session variable.

 

There is also the issue where you might want to bounce someone off the forum (or other interections with existing users). Easy to do if they need to check the users database each time they return a screen, but not so easy if once logged on they are just referring to the object.

 

All the best

 

Keith

Link to comment
Share on other sites

Im still trying to understand classes...

 

If designing a social network...

You Would have a userclass...

If a user is logged in, on the top of every page i want to have the users full name and some mroe of his details.

 

The user class would firstly query the database to see if the user is logged in, and then query the database to get

 

that users details so that they could be displayed.

 

However would you not store this user_Object in a session so that you dont have to keep querying the database, or

 

would you simply keep querying the database to get the users details on every page?

regards J.

 

Link to comment
Share on other sites

However would you not store this user_Object in a session so that you dont have to keep querying the database, or

 

would you simply keep querying the database to get the users details on every page?

 

For that I would personally just query the database. Almost certainly not have a users class, just store the data in normal variables once retrieved from the database. The data could probably be returned with a simple and fast SQL query.

 

Comes down to storage vs processing. You can basically use up one or the other. If you wanted to retrieve the details every second (say a chat form) then it would probably be worth storing it and saving on the processsing (but then the chat form probably wouldn't need that level of data). Against that if checks are every few minutes after someone has read a page then no point in clogging the storage, you would just retrieve it when needed.

 

All the best

 

Keith

Link to comment
Share on other sites

Keith i really appreciate your continued support... although i have been getting so many conflicting responses regarding when to use classes etc im some what confused... however i take your word as gospel!...

 

So in a social network scenerio, like facebook...

To get a logged in users details to be printed at the top of every page... you would simply use an include statement that runs a query to see if the user is logged in and if so grab his details...

Would it not be best still to create a user class to store the details in the user object,

but just not store the object created in a session?...

therefore the session is not stored on the server... but i have a simple way to see if the user is logged in and then if he is to grab their details?...

or you would just say no classes at all for that?...

I'm sorry to go on... but if you wouldn't have a user class in an application like facebook... when would you use a class in a social network like facebook... regards J.

Link to comment
Share on other sites

Hi

 

I am not that keen on the whole idea of using objects for everything. Others are far more keen on the idea. My view is that they are useful for storing a load of info which they will manipulate and possibly output, and which is fairly self contained. To me objects are a tool to be used when they make life simpler / more efficient, not something that should be used as a matter of course (but then my background is originally from programming mainframes in PL/1 and Cobol).

 

However with whether someone is logged on I would just store their userid in a session variable. When a panel is requested pick up that session variable and retrieve the required info (such as when they last requested a panel, which makes it easy to time them out or boot them off). Can be done in one place to check if they are logged in.

 

Think that setting up a class for the user with methods to check they are logged in, etc, is a bit of an overhead although one way of doing it. You would still need to fire off a method on the object when instantiated to check the database to see if they are still logged on so the savings in database access are small. And it seems pointless to call a method to get a value from an object when you can have the value as a variable.

 

While you could use an object to store all the info for a user on such a site (such as all their friends), there is too much chance that this data might change when they are logged on that you would still need to rebuild the object often. Eg, if you object contained a list of people you have sent friend requests to you would need to check each time if they have confirmed the request. So yes I might have a person object (with a list of friends which could also be person objects) but I would build it when required and not try and store it.

 

There could be an argument for a kind of site object storing common info stored in a database and retrieved by all scripts, but which super users can update and so rapidly pass on to other users.

 

All the best

 

Keith

Link to comment
Share on other sites

Keith,

i've always thought that using include satements are more useful than creating objects, however i'm trying my hardest to use classes so that i can gain experience.. but my biggest struggle is undestanding when to use them?...

With regard to the creating a user class to select a users friends...

I could simply have a function within the user class that gets a users friends... so i could just call it when i wanted... however i could also just have an exernal script that grabs a users friends, and use an include to grab them.

Anyway if you can give me any more examples of when you may use a class within a scoial network scenario that would be much appreciated...

thanks again.. J

Link to comment
Share on other sites

Hi

 

I feel exactly the same way with using includes and functions. To me for most uses they are more suitable.

 

One thing I would think about using a class for would be for a drop down list. Populate the class, and use it to validate whether someones response was valid, and if not return a drop down list from the class. Again though not sure I would bother to store it in a session, just recreate it

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.