object persistence in php
I am fairly new to web programming, I have mainly used java to create desktop applications in the past.
I'm trying to figure out how to create persistent objects in php. Maybe persistent isn't the right word, I don't want the object to be unique to each client, like i would get by serializing it in a session variable. I want the object to be created on the server and have that same object be accessible at all times. The object would query the database and store some data. This way, each page load, the php code would get that data from the same persistent object rather than hav开发者_运维问答ing to query the database each time.
I am currently using the singleton pattern for object creation because my initial understanding was that it would allow me to accomplish what i want. Part of the object is an array, and when i execute a php page that adds a value to the array, and access that value on the same page, its fine. However when i add a value to the array and then load another page that accesses the value, the array is back to being empty.
Is this possible? Am i overreacting in thinking that querying the database so much is bad? There will at times be as many as 20 users requesting data during any one second, and i feel like thats ridiculous to query the db each time.
Thanks
PHP does not have the concept of persistence as Java does: the JVM allows for Java applications to persist in memory between HTTP requests; the webserver forks a new PHP process every time a new HTTP request is served so an object's static variables will not persist data for you between requests.
Use a database to store persistent data. Web programming focuses on concurrency, so you shouldn't worry about database queries - 20 a second is few. If you reach the limits of your database you have the options to add a caching layer or "scale out" hardware by adding read-only slaves.
Usually you get your persistence by using the database. If that's a bottleneck you start caching data, for example in memcached or maybe a local file with a serialized array on your webserver.
Though it may not be the prettiest solution, but you can use SESSIONS for this matter.
class SomeObject{
public function __costructor{
$_SESSION['object'] = serialize($this);
}
}
and on another page, you can call the object simply with:
$object = unserialize($_SESSION['object']);
Though ofcourse this approach seems the easiest. It should come with utmost precaution:
Know that sessions depending on the traffic of your server should not be too large in size as many users concurrently ask for each of these sessions. Scale the size at your own discretion.
always serialize and unserialize as sessions not done so will misbehave.
What ever sails your boat. Do so at your own mindful analyzation. goodluck
Data persistence in Web programming is done thru the use of cookies/sessions and passing cookie/session variables across Web page calls. Theoretically, any kind of data can be stored in these variables - but for most practical purposes, only the more important data (look at them more like tokens) needed to identify/reconstruct the needed objects (with or without a database) are transferred to and from server and browser.
I'd advise you take a good look at memcached. When you're talking server load and performance capabilities, a useful metric is often pages/second. If you have a dedicated server and unoptimized but very intensive stuff going on, you may only be able to serve 5 pages/second. Utilizing data caching is a great way to increase that 3 to 10 fold. However, it's always a tradeoff as far as how stale the data can get. You will really need to test your site to properly understand (quantify) other possible performance-limiting factors such as other connections per page (images, css, etc), file i/o, other network activity, and last but not least the actual
It is possible to store objects in the current session. Now just create a base class which is able to store and recreate the object itself. Any derived object will then also be persistent.
You might want to read here : persistent base class and example
As far as i know the session is stored in RAM and thus should be faster than serialize the objects to disk to achieve persistence.
Stop using singleton and use dependency injection.
Best way is to use DataMapper (http://www.martinfowler.com/eaaCatalog/dataMapper.html) and attach it to an object by means of dynamic properties. Let the data mapper handle persistence.
$CS = new CookieStorage();
$SS = new SessionStorage();
$FS = new FileStorage('config/object.db');
$DM = new ObjectDataMapper($FS);
$O = new Object($DM);
$Object->DynamicProperty = 1;
Now DynamicProperty
will automatically persist and will automatically be loaded from file object.db
. And the class definition:
class Object
{
public function __construct(MapperInstance $Storage = NULL)
{
$this->setMapper($Storage?: new ObjectDataMapper(new FileStorage('config/object.db')));
}
public function __get($name)
{
$this->_Mapper->getProperty($name);
}
public function __isset($name)
{
$this->_Mapper->isProperty($name);
}
public function __set($name, $value)
{
$this->Mapper->setProperty($name, $value);
}
}
精彩评论