Child

We can imagine our universe made of different objects like sun, earth, moon etc. Similarly we can imagine our car made of different objects like wheel, steering, gear etc. Same way there is object oriented programming concepts which assume everything as an object and implement a software using different objects.

Object Oriented Concepts:


Before we go in detail, lets define important terms related to Object Oriented Programming.

  • Class: This is a programmer-defined datatype, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

  • Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

  • Member Variable: These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

  • Member function: These are the function defined inside a class and are used to access object data.

  • Inheritance: When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.

  • Parent class: A class that is inherited from by another class. This is also called a base class or super class.

  • Child Class: A class that inherits from another class. This is also called a subclass or derived class.

  • Polymorphism: This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.

  • Overloading: a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.

  • Data Abstraction: Any representation of data in which the implementation details are hidden (abstracted).

  • Encapsulation: refers to a concept where we encapsulate all the data and member functions together to form an object.

  • Constructor: refers to a special type of function which will be called automatically whenever there is an object formation from a class.

  • Destructors: refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

The hardest thing to learn (and teach btw,) in object oriented PHP is the basics. But once you understand them, the rest will come much, much easier.

But don't be discouraged! You just found the easiest to understand tutorial out there on OOP and PHP. It may sound like a boastful claim, I know, but that's what the nerd zeitgeist is saying.

… Or so I've been told.

About This Tutorial

This tutorial is designed to teach total beginners object oriented PHP.

This is not a long-winded theoretical blathering that you see all too often; instead, we actually start writing OO code very quickly. I believe that this 'hands-on' style of learning, makes understanding OO (object oriented) PHP much easier... and less boring too!

So before you begin, get out your favorite PHP code editor and be ready to write and run some object oriented PHP code.

PS: scroll to the bottom of the page for the supporting videos.

Thanks for reading,
Stefan Mischook

Introduction

With the release of php5 in 2004, php programmers finally had the power to code with the 'big boys'. Like Java and C#, php finally had a complete OOP infrastructure.

In this tutorial, you will be guided (step-by-step) through the process of building and working with objects using php's built-in OOP capabilities. At the same time you will learn:

  • The difference between building a php application the old fashioned (procedural) way, versus the OOP way.
  • What the basic OOP principles are, and how to use them in PHP.
  • When you would want to use OOP in your PHP scripts.

People run into confusion when programming because of some lack of understanding of the basics. The basics are really important!

With this in mind, we are going to slowly go over key OOP principles while creating our own PHP objects.  With this knowledge, you will be able to explore OOP further.