I left Microsoft Technologies and shifted to Open Source around July 2006, that’s when I was first introduced to the LAMPP Stack (Linux Apache MySQL Perl PHP), though I started my journey with Perl but gradually moved towards PHP. Open Source Technologies and PHP have been my bread and butter since then, but I could never admire PHP like I admired C, C++ and Java, always felt limited even when I was applying OOP concepts with PHP but yes in spite of this I loved PHP arrays and the wide range of array functions. My first PHP OOP project happened in December 2007 where I got introduced to Zend Framework as well, since then I’m still in love with ZF but ironically still not fond of PHP, especially PHP OOP! PHP’s weaknesses as an OOP over Java repeatedly kept disturbing me, and this became my favorite interview question for any candidate who claimed to have PHP and Java OOP concepts. I would say about 25-30% of the people could hit chord somehow but rest failed miserably.
In the first section of this article I’ll discuss about the salient features of Object Oriented Programming, next section I’ll address the broad differences between PHP & Java, and in the final section I’ll try to lay down the key weaknesses of PHP OOP over Java.
Object Oriented Programming
Conventional or traditional programing using a high level programming language like C is termed as Procedure Oriented Programming where things happen sequentially, i.e., reading (input), computing, printing (output) take place in sequences. Functions or procedures are written to accomplish all kinds of computations, some functions are system defined and some are user defined.
Object oriented programming emphasizes more on data and data structures rather than functions or procedures; they are tied with the data on which they operate. Data is defined as objects and objects are supported by member functions which are part of the data structure. Object oriented programming takes a bottom-up approach in contrast with the top-down approach of procedure-oriented programming where small modules consisting of data and member functions unite to implement a large application.
Concepts or Principles
- Objects
- Classes
- Data Abstraction & Encapsulation
- Inheritance
- Polymorphism
- Dynamic Binding
- Message Passing
Objects
Objects are the building blocks of any object-oriented system, they are the smallest units created on run-time and they represent a definable entity like a person, a car, an employee, an animal, a song, an item, any custom data defined by the user. Any problem that has to be solved programmatically must be analyzed in terms of objects if we are adopting object-oriented programming approach. While defining the objects we must try to map them with real life objects / items. When objects are created on run-time memory is allocated to store them, objects are somewhat similar with data structures in C but unlike data structures objects have member functions / methods associated with them which manipulates them and also lets them interact / communicate with other objects.
Classes
As I said objects consist of data and member functions to manipulate the data, classes are used to define the object and the functions. A class is like a custom user defined variable type with certain attributes and functionality attached to it. Objects are singular or multiple instances of a class. So, if person is a class with attributes like name, dob, height, weight and member functions like age() and bmi() then age() will calculate the age of each person based on dob and current date and bmi() will calculate their body mass index based on height and weight.
Data Abstraction & Encapsulation
Class is a combination of data and functionality and this very feature is termed as Encapsulation. Data that belongs to a specific class cannot be accessed by any outside function or the program by default which is not part of that class, i.e., only member functions can access the data of any class unless access is given specifically. This insulation of data from the program is termed as Data Hiding or Information Hiding. Also the algorithm / logical explanations of the member functions are hidden from the outside program of any class; this feature is referred as Abstraction. As Classes use the concept of Abstraction they are called as Abstract Data Types (ADT).
Inheritance
As the term suggests a Class can inherit the properties of another Class. It can implement the concept of hierarchical classification, e.g., Gorilla is type of Ape which is again a type of Mammal. So, all Gorillas will inherit the common properties of Apes and all Apes will inherit the common properties of Mammals. The concept that inheritance implement is Reusability, i.e., once we have defined the properties of Mammals and we don’t have redefine / re-write then in Apes all we have to do is establish inheritance between Mammals and Apes such that Mammals act as the parent Class of Apes, further we’ll have to append additional properties of Apes to extend its functionality. The parent Class is called as Super Class and the child Class is called Sub Class.
Polymorphism
It’s a Greek term which means “the ability to take different form”, Polymorphism is implemented in form of Function Overloading and Operator Overloading. Using Polymorphism it is possible to make a certain member function or an operator act differently based on the data input provided. The + operator can produce sum of two numbers if the operands are numeric in nature and the same operator can produce a concatenated string is the operands strings. Similarly if we define a function called getVolume() which will compute volume of a cylinder if for inputs radius and height and the same function may be re-defined to compute volume of a cube for input edge. Polymorphism is extensively used to implement inheritance, e.g., both cylinder and cube are 3D Objects so they are part of the same Super Class but since their volume is computed differently using different data points so two inherited Sub Classes were required to be defined.
Dynamic Binding
Binding refers to the linking of a procedure call with the code to be executed in response to the call, Dynamic Binding (also called as Late Binding) means that the code to be executed for a particular procedure call is not known until it is called in run-time. This is associated with Polymorphism and Inheritance. A function call associated with a polymorphic reference depends upon the dynamic nature of that reference. In the above example the method getVolume() will calculate the volume of cylinder or cube or cone or prism or sphere will depend on the call reference and getVolume() method will have to be re-defined for each type of 3D Object.
Message Passing
The process using which objects communicate with each other is called Message Passing. It simply means that (at a very abstract level) the fundamental mechanism of program execution is, objects sending each other messages. Name and structure of these messages is not necessarily fixed beforehand in the source code and can itself be additional information. C++ and Java implement a limited version of message passing through method calls where number of messages is limited to the number of methods declared in a class.
Leave a Reply