Changes

PHP Object Oriented Programming

3,039 bytes added, 19:04, 6 June 2007
New page: PHP provides extensive support for developing object-oriented software. The area of object oriented programming is, however, large. Entire books can, and indeed have, been dedicated to the...
PHP provides extensive support for developing object-oriented software. The area of object oriented programming is, however, large. Entire books can, and indeed have, been dedicated to the subject. As such, a detailed overview of object oriented software development is beyond the scope of this PHP book. Instead we will introduce the basic concepts involved in object oritent programming and thyen move on to explaining the concept in related to PHP development.

== What is an Object? ==

An object is a self-contained piece of functionality that can be easily used, and re-used as the building blocks for a software application.

Objects consist of data variables (called ''members'') and functions that can be called on the object to perform tasks (called ''methods'').

== What is a Class? ==

Much like a blueprint or architect's drawing defines what an item or a building will look like once it has been contructed, a class defines what anm object will look like when it is created. It defines, for example what the ''methods'' will do and what thge ''member'' variables will be.

== How is an Object Created from a Class? ==

The process of creating an object from the class 'blueprint' is called instantiation. Essentially you instiate an isntance of the class and give that instance a name by which you will refer to it when acessing members and calling methods. You can create as many object instances of a class as you desire. Objects are instantiated using the new keyword. For example to create an instance of a class called ''bankAccount'' you would write:

<tt>$accountObject = new bankAccount();</tt><br>

In the above example we now have an object called ''$accountObjhect'' of type ''bankAccount''.

== What is sub-classing? ==

It is possible to build class that are derived from other classes, extending the functionality of the parent class to make it specific to a particular requirement. For example you might have a ''vehicle'' class which contains the attributes common to all vehicles, and a sub-class called ''car'' which inherits all the generic ''vehicle'' attributes but adds some its own car specific methods and properties.

== Defining a PHP Class ==

Before an object can be instantiated we first need to define the class 'blueprint' for the object. Classes are defined using the ''class'' keyword following by braces which will be used to enclose the body of the class:

<pre>
<?php
class bankAccount {
}
?>
</pre>

We have now defined a class. The next step is add some functionality to the class.

== Class Constructor and Destructor ==

The next step in creating a class is to define what should happen when an object is first instantiated using the class, and also when that object is later destroyed. These actions are defined the ''constructor'' and ''destructor'' methods of the class.

The constructor and destructor are really just functions that get called when the object is created and destroyed and are defined in the class body using the ''function'' keyword. This needs to be prefixed wit