#native_company# #native_desc#
#native_cta#

OO Design: Abstract Classes Page 8

By Jonathan Griffin
on February 10, 2003

We’ll conclude our example by showing how this sort of design can be implemented into our web application.
Imagine we have a screen that lists the various automated devices throughout our house. Each device listing
has a checkbox associated with it. This allows the user to select which device(s) to control. Upon selecting
the device(s), the user can select on one of three commands: ON, OFF, STATUS.
In the processing script, the command is processed for all devices selected. The processing script is
unaware which protocol each devices has implemented. It just knows that the devices have implemented the
Control interface, so it can reliably call on(), off(), or getStatus(). The
following code shows the processing script that is called once the user makes the selection(s) and sends a
control command.

<?php

// Loop through each selected device, processing command.

foreach($_POST["devices"] as $deviceID => $selected) {

  
$device Device::getDevice($deviceID);

  switch ( $_POST["command"] ) {

    case 
"on":

      
$device->on();

      break;

    case 
"off":

      
$device->off();

      break;

    case 
"getStatus":

      
$device->getStatus();

  }

}

?>



Conclusion

Well that concludes our discussion of abstract classes. By now you should have a good understanding of what
exactly abstract classes are, as well as the knowledge to put them to use. Be sure to study the design of
various PHP applications that are available on the ‘net. You’d be surprised how many developers are already
putting them to use in PHP today.
The code presented in this article is available for download. Take a look at each example to see how these
concepts come together. If this article helped, let me know. Until next time… [editor’s note: there was a little confusion regarding the sample code. I’ve asked Jonathan to resend it and will post it here ASAP. 20030207 – JS]

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8