spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




What is Object Oriented Programming (OOP)?

By Tony Marston
2007-01-30


Practical Examples

Here are some practical examples which demonstrate Encapsulation, Inheritance and Polymorphism.

Encapsulation





Encapsulation The act of placing data and the operations that perform on that data in the same class. The class then becomes the 'capsule' or container for the data and operations.


Every application deals with a number of different entities or "things", such as "customer" "product" and "invoice", so it is common practice to create a different class for each of these entities. At runtime the software will create one or more objects from each class definition, and when it wants to do something with one of these entities it will do so by calling the relevant method on the relevant object.

The data held within each object at runtime cannot remain in memory for ever, so it is written out to a persistent data store (a database) with a separate table for each entity. There are only four basic operations which can be performed on a database table (Create, Read, Update, Delete) so I shall start by creating a method for each one.

class entity1
{
// class properties
var $dbname; // database name
var $errors = array(); // array of error messages, indexed by field name
var $fieldarray; // associative array of name=value pairs
var $fieldspec; // array of field specifications
var $numrows; // number of database rows affected
var $primary_key; // array of field names which make up the primary key
var $tablename; // table name

// class methods
function entity1 ()
// constructor
{
$this->tablename = 'entity1';
$this->dbname = 'foobar';

$this->fieldlist = array('column1', 'column2', 'column3', 'column4');
$this->primary_key = array('column1');

} // entity1

// class methods
function getData ($where)
// read data from the database which satisfies the selection criteria in $where
{
....

return $this->fieldarray;

} // getData

function insertRecord ($fieldarray)
// create a database record using the contents of $fieldarray
{
....

return $this->fieldarray;

} // insertRecord

function updateRecord ($fieldarray)
// update a database record using the contents of $fieldarray
{
....

return $this->fieldarray;

} // updateRecord

function deleteRecord ($fieldarray)
// delete a database record identified in $fieldarray
{
....

return $this->fieldarray;

} // deleteRecord

} // entity1


Please note the following:

  • The class constructor identifies the physical characteristics of this database table, that which makes it unique from all other database tables. The contents of the class constructor are performed automatically when the class is instantiated into an object.
  • All the table data is held in a single array of fields rather than a separate variable for each field. For a detailed explanation as to why I choose this method please read Why don't you use GETTERS and SETTERS?
  • I have not bothered including the actual code within each method as it there is too much of it. It you really want to see the code that I use then you can download it, either for my small sample application, or my full development framework.
  • There are actually many more methods than the four mentioned, but these are enough to begin with.

Each of these classes therefore acts as a 'capsule' which contains
both the data for an entity and the operations which can be performed
upon that data. This is 'encapsulation'.

Inheritance






Inheritance The reuse of base classes (superclasses) to form derived classes (subclasses). Methods and properties defined in the superclass are automatically shared by any subclass.


After writing and testing a class to deal with 'entity1' I copied it and made it work for 'entity2'. I then compared the two classes to see what code was common and could be shared, and what code was unique and could not be shared. I then transferred all the common code into a separate class known as a 'superclass'.

Firstly, to create the superclass, I changed the class name and the constructor to the following:

class default
{
// class properties
....

// class methods
function default ()
// constructor
{
$this->tablename = 'unknown';
$this->dbname = 'unknown';

$this->fieldlist = array();
$this->primary_key = array();

} // default

// class methods
function getData ($where)

....

function insertRecord ($fieldarray)

....

function updateRecord ($fieldarray)

....

function deleteRecord ($fieldarray)

....

} // default


This class cannot be used to instantiate a working object as it does not refer to a database table which actually exists, so it is what is known as an 'abstract' class.

Secondly, I altered each table class to remove the common methods and properties, and included the keyword extends to force inheritance from the superclass.

include 'default.class.inc';
class entity1 extends default
{
function entity1 ()
// constructor
{
$this->tablename = 'entity1';
$this->dbname = 'foobar';

$this->fieldlist = array('column1', 'column2', 'column3', 'column4');
$this->primary_key = array('column1');

} // entity1

} // entity1


When a subclass is instantiated into an object that object will contain all the properties and methods of the superclass as well as those of the subclass. If anything has been defined in both the superclass and the subclass, then the definition from the subclass will be used.

In my current development environment the superclass contains several thousand lines of code, but there is only one copy of this code which is inherited by several dozen table classes. Inheritance is therefore a powerful mechanism for making one copy of common code accessible to many objects instead of having multiple copies of that common code.

Polymorphism





Polymorphism Same interface, different implementation. The ability to substitute one class for another. This means that different classes may contain the same method names, but the result which is returned by each method will be different as the code behind each method (the implementation) is different in each class.


Polymorphism can only be employed where the same method names exist in several classes. This means that the same method can be used on different objects, but will yield different results as the implementation of that method is different within each class.

For example, take a series of classes called 'Customer', 'Product' and 'Invoice'. One practice I have seen which makes polymorphism impossible is to incorporate the entity name into the method name, as in:

  1. getCustomer(), insertCustomer(), updateCustomer() deleteCustomer()
  2. getProduct(), insertProduct(), updateProduct() deleteProduct()
  3. getInvoice(), insertInvoice(), updateInvoice() deleteInvoice()


The problem with this approach is that the object (the controller in MVC) which communicates with each table object (the model in MVC)needs to know the method name before it can open up that channel of communication. If each model has a unique set of method names then it must have a unique set of controllers to communicate with it.

My approach is to use a standard set of method names for standard operations, as in:

  1. getData(), insertRecord(), updateRecord() deleteRecord()


This is made easier as these methods are defined in the superclass and made available to each subclass through inheritance.

The advantage of this is that I can have one standard controller for each standard function, and this controller can work with any table class in the system. This is far better than having a separate set of controllers for each table class.

Here is some example code from one of my controllers:

....
include "$table.class.inc";
$object = new $table;
$data = $object->getData($where);
....


The contents of $table and $where are made available at runtime.

The significant point is that the name of the class (database table) is not hard-coded into the controller, it is passed as an argument at runtime. Only the method names are hard-coded, but as these method names exist within every table class by being inherited from the superclass they will always work. So, if the class name is 'Customer' the controller will obtain data from the 'Customer' table, if it is 'Product' it will obtain data from the 'Product' table, and so on.

Tutorial Pages:
» Introduction
» What OOP is NOT
» What is an Object Oriented language?
» What OOP is
» The difference between OOP and non-OOP
» Practical Examples
» Conclusion
» References


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: