Introduction to OOP in PHP

Introduction to Object Oriented Programming, OOP in PHP

Object oriented programming OOP is a programming technique which is used by many programming languages. In this technique, you have to use classes which are the user-defined data types, and you create functions and attributes according to your requirement and then to use them by creating an object or instance of that class so that you can access those attributes and operations of the class. In this tutorial, we will discuss OOP in PHP along with the basic concepts of OOPs.

Advantages:

The object-oriented programming has the main advantage of code reusability that you have to create a class for one time and then just have to create its instance where ever you want to use it.

The main concepts of object oriented programming are following:

  • Inheritance
  • Encapsulation
  • Overloading
  • Polymorphism
  • Abstraction

As we already discussed all of these topics in over previous posts but for now take a little review of all of them. Inheritance as the name suggests inheriting something from other. In inheritance, we have a super, and subclass hierarchy and subclasses are inherited from superclasses and was able to use the functionality and attributes of the super class. There are many types of inheritance as stated.

  • Single level inheritance
  • Multi-level inheritance
  • Multiple inheritances
  • Hybrid inheritance

For more there are other two important concepts in inheritance which are following:

  • Generalization
  • Specialization

Encapsulation mean data hiding in classes we have to define the scope of every object and functions. We normally have three scope resolution operators.

  • Public
  • Private
  • Protected
  • Sealed
  • Sealed internal

Overloading is used for defining the same thing with different meaning or perspective. There are two types of overloading:

  • Function overloading
  • Operator overloading

In function overloading, we have many functions with same name but different data type and in operator overloading, we can re-write the functionality of different operators like =, <, >, ||, $$, etc. We already have discussed overloading and overriding in Java.

Polymorphism means many shapes. In this, we interpret the same message differently, it is already covered in the topic of function overriding.

The polymorphic classes contain two types of functions

  • Virtual function
  • Overridden functions

OOP languages:

There are many object-oriented programming languages in which some are pure object oriented, and some have both procedural and also an object-oriented way.

Java and C# are pure object oriented languages

C++, PHP, JavaScript are not pure object oriented but have the functionality of it.

Object Oriented PHP
Object Oriented PHP

Constructor:

The constructor is default functionality in all object oriented languages which come with the classes whenever we create an object of the class the default constructor call and execute its code first without calling it.

In C++, Java and C# constructor have the same name as class name and have no data type but in PHP we have to define constructor like this:

 

Example class in PHP:

In Java, there is a bit different procedure of creating classes.

In the next tutorial, you will learn about overloading in PHP.