Object Oriented Programming
 
Classes and Objects in PHP5
 
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 and functions (called methods) that can be accessed and called on the object to perform tasks. These are collectively referred to as members.
 
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.
 
What is a Class?
 
Much as a blueprint or architect's drawing defines what an item or a building will look like once it has been constructed, a class defines what an object will look like when it is created. It defines, for example, what the methods will do and what the 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 instantiate an instance of the class and give that instance a name by which you will refer to it when accessing 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 employee_details we would write:
 
$emp = new employee_details();
 
In the above example we now have an object called $emp of type employee_details.
 
What is sub-classing?
 
It is possible to build classes 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 subclass 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 followed by braces which will be used to enclose the body of the class:
 
<?php
class employee_details
{ //variables, methods and other class elements }
?>
 
We have now defined a class.
 
Note: the structure of class in PHP is almost same to the classes found in other Object Oriented Programming languages such as Java/C++ etc.