Searching...
Tuesday 23 July 2013

Key concepts of OOP in PHP


In the previous post on Benefits of Object-Oriented Programming I focused three of OOP’s foundational concepts: encapsulation, inheritance, and polymorphism. Now it is time to tell you what it really means in the world of PHP.

Classes

Our everyday environment consists of countless entities: plants, people, vehicles, food...I could go on for hours just listing them. Each entity is defined by a particular set of characteristics and behaviors that ultimately serves to defineclass in php the entity for what it is. For example, a vehicle might be defined as having characteristics such as color, number of tires, make, model, and capacity, and having behaviors such as stop, go, turn, and honk horn.

In the vocabulary of OOP, such an embodiment of an entity’s defining attributes and behaviors is known as a Class. The set of characteristics and behaviors are better known in OOP as properties and methods, respectively

Classes are intended to represent those real-life items that you’d like to manipulate within an application. For example, if you want to create an application for managing a public library, you’d probably want to include classes representing books, magazines, employees, special events, patrons, and anything else that would require oversight. Each of these entities embodies a certain set of characteristics and behaviors, better known in OOP as properties and methods, respectively, that define the entity as what it is. PHP’s generalized class creation syntax follows:

class ClassName
{
// Property declarations defined here
// Method declarations defined here
}


Listing 6-1. Class Creation - a class representing employees

class Employee
{
private $name;
private $title;
protected $wage;
protected function clockIn() {
echo "Member $this->name clocked in at ".date("h:i:s");
}
protected function clockOut() {
echo "Member $this->name clocked out at ".date("h:i:s");
}
}

Titled Employee, this class defines three properties: name, title, and wage, in addition to two methods, clockIn and clockOut. Don’t worry if you’re not familiar with some of the syntax; it will become clear later in the articles that follows.

Note: While no official PHP code conventions exist, consider following the PHP Extension and Application Repository (PEAR) guidelines when creating your classes. You can learn more about these conventions at pear website.

Objects

A class provides a basis from which you can create specific instances of the entity the class models, better known as objects. For example, an employee management application may include an Employee class. You can then call upon this class to create and maintain specific instances, Sally and Jim, for example.

Note: The practice of creating objects based on predefined classes is often referred to as class instantiation.

Objects are created using the new keyword, like this:

$object_name = new class_name()

For Example:

$employee = new Employee();

Once the object is created, all of the characteristics and behaviors defined within the class are made available to the newly instantiated object. Exactly how this is accomplished is revealed in the given articles:-


Classes form the structure of data and actions and use that information to build objects.

Constants

You can define constants, or values that are not intended to change, within a class. These values will remain unchanged throughout the lifetime of any object instantiated from that class. Class constants are created like so:

const NAME = 'VALUE';

For example, suppose you create a math-related class that contains a number of methods defining mathematical functions, in addition to numerous constants:

class mathFunctions
{
const PI = '3.14159265';
const E = '2.7182818284';
const EULER = '0.5772156649';
// Define other constants and methods here...
}

Class constants can then be called like this:

echo mathFunctions::PI;


3 comments:

 
Back to top!