Searching...
Monday 22 July 2013

Benefits of Object-Oriented Programming


Although PHP did not start out as an object-oriented language, over the years a great deal of effort has been put into adding many of the object-oriented features found in other languages. This article and the following aim to introduce these features. Before doing so, let’s consider the advantages of the OOP development model.

Object Oriented PHP

Just a thought: If you got any OOP history before PHP this post is going to be a tom’s task for you- very easy. But if you are new to OOP concept and you got confused at any point, don’t get disheartened because as you will proceed to your journey with OOP features of PHP you will realize what I want to say here. Because  things can very easily be understood in the field rather than the theory.

The Benefits of OOP

The birth of object-oriented programming represented a major paradigm shift in development strategy, refocusing attention on an application’s data rather than its logic. To put it another way, OOP shifts the focus from a program’s procedural events toward the real-life entities it is intended to model. The result is an application that closely resembles the world around us.

This section examines three of OOP’s foundational concepts: encapsulation, inheritance, and polymorphism. Together, these three ideals form the basis for the most powerful programming model yet devised.

Encapsulation

Programmers enjoy taking things apart and learning how all of the little pieces work together. Although gratifying, attaining such in-depth knowledge of an item’s inner workings isn’t a requirement. For example, millions of people use a computer every day, yet few know how it actually works. The same idea applies to automobiles, microwaves, and any number of other items. We can get away with such ignorance through the use of interfaces. For example, you know that turning the radio dial allows you to change radio stations; never mind the fact that what you’re actually doing is telling the radio to listen to the signal transmitted at a particular frequency, a feat accomplished using a demodulator. Failing to understand this process does not prevent you from using the radio because the interface gracefully hides such details. The practice of separating the user from the true inner workings of an application through well-known interfaces is known as encapsulation.

Object-oriented programming promotes the same notion of hiding the inner workings of the application by publishing well-defined interfaces from which each application component can be accessed. Rather than get bogged down in the gory details, OOP-minded developers design each application component so that it is independent from the others, which not only encourages reuse but also enables the developer to assemble components like a puzzle rather than tightly lash, or couple, them together. These components are known as OBJECTS, and objects are created from a template known as a CLASS, which specifies what sorts of data the object might contain and the behavior one would expect. This strategy offers several advantages:
  • The developer can change the application implementation without affecting the object user because the user’s only interaction with the object is via its interface.
  • The potential for user error is reduced because of the control exercised over the user’s interaction with the application.

Inheritance

The many objects constituting our environment can be modeled using a fairly well-defined set of rules. For instance, all employees share a common set of characteristics: name, employee ID, and wage. However, there are many different types of employees: clerks, supervisors, cashiers, and chief executive officers, among others, each of which likely possesses some superset of those characteristics defined by the generic employee definition. In object-oriented terms, these various employee types inherit the general employee definition, including all of the characteristics and behaviors that contribute to this definition. In turn, each of these specific employee types could be inherited by yet another more specific type. For example, the Clerk type might be inherited by a day clerk and a night clerk, each of which inherits all traits specified by both the employee definition and the clerk definition. Building on this idea, you could then later create a Human class, and then make the Employee class a subclass of Human.

The effect would be that the Employee class and all of its derived classes (Clerk, Cashier, Executive, etc.) would immediately inherit all characteristics and behaviors defined by Human.

The object-oriented development methodology places great stock in the concept of inheritance. This strategy promotes code reusability because it assumes that one will be able to use well-designed classes (i.e., classes that are sufficiently abstract to allow for reuse) within numerous applications.

Polymorphism

Polymorphism, a term originating from the Greek language that means “having multiple forms”, defines OOP’s ability to redefine, or morph, a class’s characteristic or behavior depending upon the context in which it is used. Returning to the example, suppose that a behavior titled clockIn was included within the employee definition. For employees of class Clerk, this behavior might involve actually using a time clock to timestamp a card. For other types of employees, Programmer for instance, clocking in might involve signing on to the corporate network. Although both classes derive this behavior from the Employee class, the actual implementation of each is dependent upon the context in which “clocking in” is implemented. This is the power of polymorphism.
These three key OOP concepts (encapsulation, inheritance, and polymorphism) are further introduced as they apply to PHP through the next articles.

Note: While it may be daunting at first, OOP actually provides an easier approach to dealing with data than the procedural programming.


0 comments:

Post a Comment

 
Back to top!