Ferodynamics Network

popular: profile privacy, mobile privacy

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
26 comments
page 1065
Quick N Dirty Admin Login Screen
no comment
page 128
Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Beware Wp Cache
8 comments
page 1310
Wordpress Chat
one comment
page 1308
Wp Polls Reviewed
one comment
page 58
Fun With Photo Data
12 comments
page 330
Html 5 Gallery
6 comments
page 1305
Fun With Sidebar Tabs Styling
2 comments
page 336
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
My Experience Of Flexx
4 comments
page 1026
Fun With Sidebar Tabs
193 comments
page 57
Html 5 Gallery
6 comments
page 1305
Fun With Plugins
27 comments
page 14
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
  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 [...]