888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

January 20, 2008

In part 2 of the series I looked at the way objects can be used to separate the repetitive logic from the core code of the plugin itself by demonstrating an object based text box. In this part I’m going to take it one further and show how several objects can be derived from a common class to prevent repetition of code in the objects themselves.

I ended part 2 with two classes: A text box class that handled display of a textbox as well as getting and setting its value when the form is loaded or submitted, and the class used for the plugin itself that created an admin page and added three textboxes.

In this part I am going to create two more classes. The first is a generic form control class. Into this I am going to put the initial declaration of the necessary variables, and the value getting and setting code from the text box class.

This is the full text box class from part 2:

[php]
//note this example does not perform any validation.
class my_text_box
{

var $prefix = 'my_text_box_';
var $name = '';
var $value = '';
var $description = '';

//php 4 constructor
function my_text_box( $name , $description ) {

$this->name = $name;
$this->description = $description;

if ( $v = get_option( $this->prefix . $this->name ) ) {
$this->value = $v;
}

if ( isset( $_POST[$this->prefix . $this->name] ) ) {
$this->value = $_POST[$this->prefix . $this->name];
update_option( $this->prefix . $this->name , $this->value );
}
}

function display() {

echo '

';
echo '';
echo '
';
echo '

';

}
}

The new class, is a generic class that doesn’t display anything, but does handle the values. You can see the sections of the code that have been removed from the textbox class by comparing the two classes.

In the generic class the value getting and setting code is no longer part of the constructor, so it will need to be called explicitly. Also note I have amended the prefix from my_text_box to the more generic my_form_control.

[php]
class my_form_control{

var $prefix = 'my_form_control_';
var $name = '';
var $value = '';
var $description = '';

function get_value( ) {

if ( $v = get_option( $this->prefix . $this->name ) ) {
$this->value = $v;
}

if ( isset( $_POST[$this->prefix . $this->name] ) ) {
$this->value = $_POST[$this->prefix . $this->name];
update_option( $this->prefix . $this->name , $this->value );
}
}
}

Now, this is the important part. Having moved some of the code into a generic class we need to rewrite the textbox class, and we do this by using the ‘extends’ keyword.

[php]
class my_text_box extends my_form_control
{

//php 4 constructor
function my_text_box( $name , $description ) {

$this->name = $name;
$this->description = $description;
$this->get_value();
}

function display() {

echo '

';
echo '';
echo '
';
echo '

';

}
}

The ‘extends’ keyword, used in the class title tells PHP that our class should inherit everything that is in the generic class, so although the my_text_box class is now much shorter than it was it actually has exactly the same functionality.

As well as moving the code into the generic class there is one more change; the constructor of the text box class now calls the get_value function. Note that we can still use the $this keyword to access it, even though it is contained within the generic class we have extended.

The benefit of this is that we can now create a second type of form control which will differ in only one way, it will output slightly different html, and we don’t need to recreate the code that gets and sets the value.

[php]
class my_text_area extends my_form_control
{

//php 4 constructor
function my_text_area ( $name , $description ) {

$this->name = $name;
$this->description = $description;
$this->get_value();
}

function display() {

echo '

';
echo '';
echo '';
echo '

';

}
}

This new class works in exactly the same way as the text box class, except that it outputs the html for a textarea, instead of a text box. It extends the base class, uses the same method of getting and setting the value. Already, extending the generic class has saved 10 lines of code that would have been identical to each other. If we were to create a third class, for example, a drop down box, then we would have saved twenty lines of code. Add that the fact that if you find a flaw you only need to amend it once, and the saving made by using objects instead of hardcoding the html into the admin page and you start to see how beneficial using objects can be.

Finally, there is one more change that is needed. We need to amend the actual admin page itself to use a text area as well as a text box. To do this we change only one line from the admin page in part 2:

[php]$option2 = new my_text_box( 'Option_2' , 'Second Option');

We change this to:

[php]$option2 = new my_text_area( 'Option_2' , 'Second Option');

So the final admin page class looks like this:

[php]
class my_plugin
{

//PHP 4 style constructor
function my_plugin(){

add_action( 'admin_menu' , array(&$this , 'add_admin_menu') );

}

function add_admin_menu(){

add_options_page('My Plugin Options', 'My Plugin Options', 9, basename(__FILE__), array(&$this, 'admin_page'));

}

function admin_page(){

$option1 = new my_text_box( 'Option_1' , 'First Option');
$option2 = new my_text_area( 'Option_2' , 'Second Option');
$option3 = new my_text_box( 'Option_3' , 'Third Option');

?>

My Admin Page

display(); ?>

display(); ?>

display(); ?>

}

}

$my_plugin = new my_plugin();

We've made a significant change to the admin page by changing only one word of the actual page content itself.

You can download the finish plugin here: just remove the .txt from the end of it.

In this part I have demonstrated how you can extend generic classes that encapsulate regularly used functions to create multiple classes with similar functionality without repeating the same code, potentially saving a significant amount of identical code and making the code more maintainable at the same time. In the final part I will look at ways of storing collections of objects in order to remove even the admin page code itself from the core plugin files.

Post Image The Easy Peasy Way
Get post images?
26 comments
page 1065
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress get first image large?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress create your own framework?
2 comments
page 335
Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
How To Add Sidebars To A Theme
How to add pages in footer wordpress?
11 comments
page 1053
Post Image The Easy Peasy Way
How to post all gallery images in one post wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Get attachment by post?
26 comments
page 1065
Html 5 Gallery
Html 5 tab?
6 comments
page 1305
Wordpress 25 Exif Fields
Exif info display wordpress?
12 comments
page 230
Using Your Own Url Shortener
Tiny urls htaccess?
4 comments
page 1190
Six Million Ways To Die Choose One
6 million ways to die so i chose?
14 comments
page 1128
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Fun With Sidebar Tabs Styling
Funwithsidebartabs customize css?
2 comments
page 336
Wpunlimited The Ultimate Wordpress Theme
Ultimate admin theme?
3 comments
page 1141
Quick N Dirty Admin Login Screen
Css login screen?
no comment
page 128
My Experience Of Flexx
Flexx theme review?
4 comments
page 1026
Fun With Sidebar Tabs Styling
Css tabcontentcontainer?
2 comments
page 336
Html 5 Gallery
Html 5 e wordpress?
6 comments
page 1305
Dont Mess With My Toot Toot
Toot html5?
16 comments
page 599
Using Your Own Url Shortener
How to create your own url shortener?
4 comments
page 1190
Dont Mess With My Toot Toot
Wordpress custom content types?
16 comments
page 599
Using Your Own Url Shortener
Funny url shortener?
4 comments
page 1190
Theming Habari Vs Wordpress
How to create a habari theme?
13 comments
page 440
Post Image The Easy Peasy Way
Addd multiple images to post wordpress?
26 comments
page 1065
Upload From Url
Upload by url?
6 comments
page 326
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Add image url to page data wordpress?
26 comments
page 1065
Six Million Ways To Die Choose One
Six million ways to die?
14 comments
page 1128
Html 5 Gallery
Html 5 foto gallery?
6 comments
page 1305
Wpunlimited The Ultimate Wordpress Theme
Html 5 photo gallery?
3 comments
page 1141
Html 5 Gallery
Image galleries in html5?
6 comments
page 1305
  1 query every 1119 seconds, updated 1 seconds ago.
Monday, 6am
 __
(__)
   `

 ???????

9xI’ll thingk about it.2d I compleatly disagree with last post . nsk
?????? ? ??????? 1i

Monday, 2am
 __
(__)
   `

 WordPress ??????? – WordPress???

[...] ???????????????????????a memerb of a class????????????????????????????? [...]

Tuesday, 5pm
 __
(__)
   `

 Marulz Blog » Blog Ar?ivi » Weblog Tools Collection: Error Management for WordPress Plugins

[...] plugins, it’s best to have your errors as a member of a class. Using the class approach assures that you can access the errors throughout your methods, and also [...]

Sunday, 10pm
 __
(__)
   `

 Weblog Tools Collection » Blog Archive » Error Management for WordPress Plugins

[...] plugins, it’s best to have your errors as a member of a class. Using the class approach assures that you can access the errors throughout your methods, and also [...]