PHP and Object Oriented Programming

Object-Oriented Programming is a principle to visualize the problem in the form of small objects. In this approach, the programmer defines the data-type, which includes the data and the functions into a single unit.

It aims to implement real-world entities such as data hiding, polymorphism, inheritance, etc. using programming.

PHP version 5 started to implement object-oriented principles. Which enhances the development of more complex, modular and reusable applications.

PHP with object oriented programming enhances the power by

  • Writing manageable code.
  • Prevents the repetition of codes.
  • Decrease development time
  • Efficient coding.

Now PHP accepts the principles of object-oriented programming.

Principles of Object-Oriented Programming

Principles of Object Oriented Programming
  • Class: A user-defined data type, which includes functions as well as data. A blueprint for creating objects. It encapsulates the data and functions into a single unit.
  • Object: An individual instance of the user-defined data type defined by a class. It takes space in memory. Objects can interact with each other without knowing the basic details of each other such as data and functions. Each object has its data and functions. For example, Pen is a class and ballpoint pen and gel pen are the objects of the same pen class.
  • Abstraction: Showing only required information to the outside world.
  • Encapsulation: The process of combining data and methods as one entity. It creates insulation between the outside world and the data. It means
  • Inheritance: Sharing properties or behavior into different groups. For example, the child inherits properties from its parent. It enhances the reusability of code.
  • Polymorphism: Ability to change the behavior as per the situation.
  • Overloading: A type of polymorphism in which some or all operators have different implementations depending on the types of their arguments.
  • Constructor: A function that will be called automatically whenever there is an object formation from a class. It allocates the memory for objects dynamically or in runtime.