Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding PHP > PHP - Object-oriented programming (OOP) Introduction
Welcome on Frogg's web tools | Current date :
21/12/2024

PHP - Object-oriented programming (OOP) Introduction

Object Oriented Programming in PHP is faster and easier to execute.

Object oriented programming (OOP) was first introduced in php4. Only few features of OOP were available in php4. Advance features and major concept of the object oriented programming in PHP is introduced from version 5 which is commonly known as php5.

Object-oriented programming is a style of coding that allows developers to organize the data and structure of an application into classes.
One of the major benefits of DRY (Don't Repeat Yourself) programming is that, if you need to change some information in your program then usually only one change is required to update the code.

Using oops concept in php, you will get lots of advantage and it gives you standard way to re-usability.

Its easier to maintain large application, suppose websites are developed by using normal programming techniques. In that scenario you will have to analyse that how many places you have defined code for same use after finding all places you are going to made changes, so its going very difficult for you and let's think about OOP technique you just need to change in your functions of class and after changes it will be automatically reflect where you call this method.

You can create seperate class for each module then you will have to change only in their business logic part. This process is known as Molecularity

Using OOP concept in PHP, you can avoid repeating of code, which means once you have write a couple of code in your method then you can re-use it in your page by calling the function of class.
As in real life, you can notice that everything it means whole universe is made of different objects such as PANCHABUTA or FIVE ELEMENTS (Earth, Water,Fire, Air, Ether).

Similarly, we can take example of car which is made of different objects like wheel,steering, gear.

Same way php oops concepts assume everything as an object.

Before knowing about basic concept of object oriented programming, know the main pillers of OOP(ObjectOriented Programming) which is mention below :
  • Class and Object
  • Constructor/Destructor
  • Encapsulation
  • Abstraction
  • Polymorphism
  • Inheritance
  • Visibility
Source : www.expertphp.in

OOP Vocabulary

  • Class
    A Class is an new type with its main structure that is composed by its attributes & methods.
  • Attributes
    An attribute is a variable/constant in a class.
  • Constants
    A constant declared in a class.
  • Methods
    A method is a function in a class.
  • Arguments
    An argument is a method parameter.
  • Object
    An object is an instance of a class.
  • Instantiation
    An instantiation is the creation of a distinct object based on a class.
  • Class members
    A class member is a method or constant or attribute of this class, all class members are all methods, constants and attributes of it.
  • Construtor
    Is the class main method automatically called when an Instantiation is done.

Simple comparison between Object-Oriented Programming(OOP) and Procedural programming

A simple way to compare both programming methods is to think of Object-oriented Programming as learn to read picture book. As children see pictures of simple objects like a house or picture they know that throughout the book when they see a picture of the house it represents the word house. The children can then read through the book as words are substituted throughout the story with pictures.

In Object-oriented Programming the classes could represent the pictures in the learn to read books. A house could represent a class and anything the developer wants to have included to describe that house like the color, size, number of bathrooms etc.

In Procedural Programming the learn to read book would be words on the page without pictures to help guide the young learner through the book. If the story was changed in the beginning of the book it could disrupt or make the story later on in the book not make any sense. Although learning to read the book would make programming with Procedural Programming simple, it would make it difficult for other readers in the case of the book or programmers in the case of Procedural Programming to add to the story.

Procedural Programming also uses different methods throughout the code than Object-oriented Programming.

As an example Object-oriented Programming uses methods where Procedural Programming uses procedures.

Object-oriented Programming uses objects where Procedural Programming uses records.

Object-oriented Programming uses classes where Procedural Programming uses modules and Object-oriented Programming uses messages where Procedural Programming uses procedure calls.

In addition, Object-oriented Programming uses data fields where Procedural Programming uses procedures. A chart has been provided below to illustrate the differences between the two:

Procedural Object-oriented
procedure method
record object
module class
procedure call message
Source : neonbrand.com
based on : wikipedia.org

OOP Advantages

Constructor/Destructor

Constructor

Constructor is a key part of PHP oops concept.
Constructor in PHP is special type of function of a class which is automatically executed as any object of that class is created or instantiated.
Constructor is also called magic function because in PHP, magic method is start usually with two underscore characters.
In PHP4, we create constructor by class name it means the name of constructor is same as the class name but in PHP5, it has been changed to create constructor, it can be initialized by __construct keyword.

Source : www.expertphp.in

Destructor

Destructor in PHP is similar to other oops(object oriented programming) concept.
PHP Destructor is handled by the Garbage Collector which always focus over object when there are no needed of object then it automatically destroyed.
Destructor function are inverse of Constructor. The method name of Destructor in php is differ from Constructor method name and it can't take any arguments like constructor.
Destructor function name in PHP is __destruct().
Constructor is involved when objects are created and Desctructor is involved when object are deleted.

Source : www.expertphp.in

Encapsulation

Encapsulation is an OOP (Object Oriented Programming) concept in PHP.

Wrapping some data in single unit is called Encapsulation. Encapsulation is used to safe data or information in an object from other it means encapsulation is mainly used for protection purpose.

In technology era, it is essential to maintain your privacy so for security we are making private method sometime.

Private method means it can be accessed withing same class. Out side class can't access private method of other class.That's why encapsulation is known as data hiding is the main advantage for encapsulation.

Second advantage of encapsulation is you can make the class read only or write only by providing setter or getter method.

Capsule is best example of Encapsulation. Capsule basically encapsulate several combination of medicines. Schoolbag is one of best example of Encapsulation. School bag can keep our books, pen etc.

So finally the concept of Encapsulation in PHP is hiding internal information of object to protect from the other object.

Source : www.expertphp.in

Abstraction

Concept of Abstraction in PHP is important php oop (object oriented programming) concept.

To show the needed/relevant information or details without showing all information which is not necessary is called abstraction.

Abstraction focus only what the object does instead of how.

Consider a very simple example in mathematics world - mulitplication. If we have two variables 'x' and 'y' then we simply say their multiplication without knowing what happening behind the scenes it means how variable value are stored in memory, how variable value convert in binary format, how processor processes the multiplication information.

Source : www.expertphp.in

Polymorphism

Polymorphism is basically derived from the Greek which means 'many forms'.

In other words, polymorphism is what join bunch of classes with one interface. polymorphism is a key part of php oops.

As you can see in above image, printer class has a print method which performs multi task, such as print to screen and print to paper.

Also, you can take example of twin brothers, they look like same but have different characters.
  • Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.

Source : www.expertphp.in

Inheritance

Inheritance is the php oops concept which is based around the concept of base classes or superclasses and derived classes or subclasses.

Base classes or Super Classes are also known as parent classes and similarly, derived classes or subclasses are known as child classes.

In the real world as a child take features from their parents, Same way in oops, child class inherit properties of their parent class with the help of inheritance.

We use `extends` keyword to implement inheritance in php.

Advantage of using inheritance in php oops, we can share properties of one class to other class.

Source : www.expertphp.in
Page created by the 17/02/2018 19:27
Generated in 0.001 sec & displayed in ... sec