PHP OOPS Concept

PHP OOPS Concept

OOPS Concept with Real Life Example

Objects

.An object represents a particular instance of a class and it can be more than one instance of an object. Each instance of an object can hold its own relevant data. An Object is a collection of data members and associated member functions also known as methods.

Class

Class is a user defined data type that combine the variable and function together into single entity. Objects with similar properties and methods are grouped together to form a Class. Characteristics of an object are represented as Properties and actions that performed by objects becomes functions of the class and referred as Methods. It is just like a blueprint of a system.

For example: Class of Cars under which Santro, Alto,  WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Colour, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, and Stop form the Methods of Car Class.

No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.

Abstraction

Abstraction means showing essential features and hiding non-essential features to the user.

For example Yahoo Mail: – When you provide the user name and password and click on submit button. It will show Compose, Inbox, Outbox, Sent mails etc. When you click on compose it will open but user doesn’t know what are the actions performed internally. It just opens that is essential; User doesn’t know internal actions that is non-essential things.

Encapsulation

Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).

For Example: A car is having multiple parts like steering, wheels, engine etc. which binds together to form a single object that is car. So, here multiple parts of cars encapsulates itself together to form a single object that is Car.

In real time we are using Encapsulation for security purpose.

Encapsulation = Abstraction + Data Hiding.

Inheritance

Deriving a new class from the existing class, is called Inheritance. Derived (sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class.

For example: – Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount (), capacity () will be same for all of the three classes. That create duplication of same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used.

Polymorphism

Polymorphism means ability to take more than one form that an operation can exhibit different behaviour at different instance depend upon the data passed in the operation.

For Example: We behave differently in front of elders, and friends. A single person is behaving differently at different time.

Dynamic Binding

In dynamic binding, the code to be executed in response to function call is decided at runtime. C++ has virtual functions to support this.

For Example: Overriding

Message Passing

Objects communicate with one another by sending and receiving information to each other. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

Summary:

1. Object                     – Instance of Class

2. Class                       – Blue print of Object

3. Encapsulation         – Protecting our Data

4. Polymorphism         – Different behaviours at different instances

5. Abstraction             – Hiding our irrelevant Data

6. Inheritance              – One property of object is acquiring to another property of object

7. Dynamic Binding   – Run Time Calling

8. Message Passing     – Exchange of Information

One thought on “PHP OOPS Concept

Post Your Comments & Reviews

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

*

*