Object-Oriented Programming (OOP) in PHP

Follow by Email
Facebook
Google+
http://xtremepassion.leladystore.com/2011/04/object-oriented-programming-oop-in-php/
YouTube
PINTEREST
INSTAGRAM

When talked about OOP or Object-oriented programming, you should make yourself clear for below terms and usage:-

CLASS

A class is a blueprint, or prototype, that defines the variables (data/properties/ attributes) and the methods (operations/functions) common to all objects of a certain kind.

Figure1: Example of Class Structures and Product Class

Figure2: Example of coding depicted the Product class at Figure1


OBJECT

An object is an instance of a class. A class must be instantiated into an object before it can be used (accessed) in the software. More than one instance of the same class can be in existence at any one time.

Figure3: An object - instance of a Product class
Figure4: Example of coding to instantiate an object to gain access into the member variables.

Output:
Price: 48.25

ENCAPSULATION

The act of placing data and the operations that performs on that data in the same class. The class then becomes the ‘capsule’ or container for the data and operations. It refers to the hiding of data within a class (act as ‘capsule’) and making it available or visible (controlling who sees things) only through certain methods or functions. It is important to protects certain data within a class from being modified inappropriately.

Figure5: Explained on encapsulation
Figure6: Codes that demonstrated encapsulation is important to protects certain data within a class from being modified inappropriately

Output:
**** display Incremental of STATIC variable ****
11
12
13
14
15

**** display from WITHIN the class ****
PROTECTED static no: 15
PUBLIC: 20
PROTECTED: 30
PRIVATE: 40

**** display from EXTENDING class ****
PROTECTED static no: 15
PUBLIC: 20
PROTECTED: 30
PRIVATE:

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.

Figure7: Example of inheritance with base class or superclass (Product class) and derived class or subclass (DiscountedProduct class)

Output:
Discount 50 percents: 24.125

POLYMORPHISM

The word comes from Greek poly (many) and morph (form). It refers to the ability of some code to be used with multiple types of values. 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.

Figure8: Both classes (superclass and subclass) using same method names but getting different result or output

Output:
Price per Unit: 48.25
Price per Unit: 24.125

CONCLUSION:

Object-Oriented Programming or OOP is programming which is oriented around objects, thus taking advantage of Encapsulation, Polymorphism, and Inheritance to increase code reuse and decrease code maintenance.

[Terms and definition sourced from www.tonymarston.net]

Leave a Reply

Your email address will not be published. Required fields are marked *

2,041 views