November 14, 2007

So you’re writing a plugin that uses an object to maintain the state of your application, perhaps the object is used to construct a document, or a menu, or something else entirely, and your application requires multiple form submissions. How do you maintain the state of your object across submissions?

There are lots of ways to do this, use sessions, use hidden fields, recreate the object manually with each postback. Which method proves most obvious depends on your background, and experience. The most usual for PHP, is to use session variables to save the data

For the examples I’m going to create an object with four methods: getters and setters for the internal data, one to save the state and one to reload it again, exactly how and when these are called may differ depending on circumstances.

[php]
class myClass{

//the array the data will be stored in
var $collection = array();

function getVar($name){
if ( isset($this->collection[$name]) ) {
return $this->collection[$name];
}
}

function setVar($name = '', $value = ''){
$this->collection[$name] = $value;
}

function loadState(){
if ( isset( $_SESSION['myClass_state'] ) ) {
$data = $_SESSION['myClass_state'];
$this->collection = (!empty($data['collection'])) ? $data['collection'] : array();
//repeat for any other data maintained
}
}

function saveState(){
$data['collection'] = $this->collection;
//add any other internal data in the same way
$_SESSION['myClass_state'] = $data;
}

}

These methods can then be called after checking to see whether the form is actually being submitted.

[php]

$myClass = new myClass();

if ( isset( $_POST['submit'] ) ) {
$myClass->loadState();

//make any changes dictated by the information that has been posted
$myClass->setVar('post_title',$valueSubmitedInForm);

$myClass->saveState();

}

An interesting alternative to this, which I haven’t used in anger but have considered, is to use hidden forms to contain the same data. Instead of saving the information in a session variable the data is serialized, base64 encoded, and added as the value of a hidden field.

There are more security issues with doing things this way though so it is probably best avoided.



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Html5 photo galleries?
6 comments on page 1305

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1728s, 1s ago, in 0.03s.
Post a comment?

0s