views:

59

answers:

5

Hi

I'm just playing around with some PHP and was wondering what happens when an object from a class is created within another PHP script?

I assume once its created and been processed their is no way of then going back and 'playing' around with it from another script?

The idea is i'm trying to create a kind of deck of cards using a card class, each card has specific data that is added to each individual object to make it unique, suit, value etc. Once its created i need to be able to go back to specific cards to use them. In java i'd have an arraylist of card objects, i'm not sure how to approach the same area in PHP.

Thanks.

A: 

So you want to create a serverside coded cardsgame? Good luck! It is possible to do this, tho I think a script like a javascript you are talking about is much more suitable. You could make a function that initialises a deck of cards and work with indexes etc. Save your things in cookies / sessions and work with postbacks. It's gonna be a hell of a job tho in my opinion compared to jscript.

Tho when you think about it, you could use ajax to make this game feel better for the user :).

Younes
i intend to use ajax, but i'm trying to understand how i can keep the objects in the deck available to me. in java once they are created they are stored in memory in my arraylist to use. i'm trying to achieve something similar in php.
Uncle
And for that indeed you can use serialization.
Younes
+1  A: 

To reuse an object between page calls seems to be your issue. Maybe you can serialize the object and store it in database and pick it up back?? Check php.net/serialize Let know how it goes.

pinaki
this looks promising, however the idea would be to serialise a deck of cards(52 objects). The idea of serialising and un-serializing seems like a lot of work. i'll give it a shot anyway and see what happens and get back to you. thanks for the answer.
Uncle
+1  A: 

What you could do to keep the objects available to you is to serialize the objects and store them in a database table. If you link a game ID or something similar to the cards then you can retrieve them later using this game ID.

I don't know if the cardgame you are writing is realtime, using a database might be too much overhead. Another possibility is to use an existing caching solution, like for example Memcache.

Dean
this seems to be ideal. i was originally thinking using a DB and going back and forth would just seem like too much work and won't be fast enough for a real time game. suppose the proof is in the pudding.
Uncle
+2  A: 

There is no problem passing objects around inside a php script, your problem is that php is that the webserver calling the script is essentially "stateless". i.e. every time someone posts the url from a browser a complete fresh copy of the php program is fired up.

To save data between times there are several options:- One is to use $_SESSION variables which are associated with a user session but $_SESSION itself is an array so it gets really clumsy holding complex structures here, also , it sounds like you want to share the deck between users.

You could serialise your object and store it in a file -- which is OK as long as its not updated very often -- but if its updated by every user they will start overwriting each others changes.

Much better is to store the deck in a database (SQLITE is usually built into php) so that several users can share and update in a controlled manner.

Another good option would be to use one of the popular data caches such as "memcached" which will cache the data between calls to the script.

James Anderson
A: 

Php scripts are not like Java server apps. Where your Java server will run for a long time, your php script will just be a one time thing.

Instead of this kind of process : user make a request to Java-run server, server receive the request in one of it's infinite loops, server process it, server send the response, server wait for new request; you have this kind of thing : a webserver (Apache, Nginx, whatever other webserver) receive the user's request, understand it needs to be interpreted by php, starts a php child, this child do what's in the script, send its answer, dies, the server wait for new requests.

So, when a php script ends, nothing (in good case) is left from it.

But, a php script can use persistent storage on the server so another request can read from it. That's why you have files, databases and even shared memories functions.

If the games state is for one user only, you can use sessions (usually files) to store your deck object. If it's meant to be used by multiple players, you should store it after serialization in a database.

Arkh