OOP in PHP – Egg example (Part 1)

This is a quick tutorial on object-oriented programming (OOP) in PHP. There are two parts of this tutorial. I will explain OOP with the help of an egg. So let’s get started.

Target Audience: Developers who know procedural PHP but want to quickly learn about OOP in PHP.

Class

When you look at an egg you can say its an egg. Basically, you are defining that object name egg. And also that egg has properties such as big egg or small egg.

In the same fashion, a class is just a definition of an object. Every class definition begins with the keyword class followed by the class name. Then you have curly braces that surround the class definition. Here I am defining a student class and making use of two built-in methods:

  1. get_declared_classes shows list of declared classes
  2. class_exists checks if a class exist

Instance

Suppose you have a dozen eggs, which means you have a dozen objects or instances having similarities.

In general use, an instance is a single occurrence of something. When we’re talking about object-oriented programming, that single occurrence of something is a single instance created from a class definition. Instance and object are used interchangeably but technically instance is the correct term. An instance is created using new keyword followed by class name. It is also called instantiation. We will be making use of a built-in method for instances:

  1. get_class gets the class name from instance

Class Properties

Your egg can have properties e.g white egg, brown egg, etc. You can define properties inside the class definition.

Class properties are variables that hold instance values. Class properties are also called class attributes or instance attributes. Below we are setting class properties inside class definition and referring it through an instance. We will be using three built-in functions as well:

  1. get_class_vars gets class properties
  2. get_object_vars gets instance properties
  3. property_exists checks property existence in a class

Class Methods

You can do different things with egg e.g you can cook it and can have a boiled egg, fried egg, etc. These are different methods.

When we define a function inside a class definition it is called a class method. Some people call it class function but the class method is a more appropriate term. We will make use of two functions here:

  1. get_class_methods gets class methods
  2. method_exists checks method existence in a class definition

Refer an instance

You have a dozen eggs in front of you. You decide to boil the third egg. Now you are referring an instance.

Refer an instance means when we call a class method it must refer to the “instance properties” instead of “class properties”.

  1. $this-> used inside the class to refer the instance properties only. This means if there are several instances each one will have its separate instance properties.
  2. $variable_name-> used outside class to refer to object properties

Inheritance & Subclass

You have two types of eggs. One is chicken egg and the other one is duck egg. Both are called eggs. The chicken egg has some properties similar to a duck egg, that’s called inheritance. But at the same time, a chicken egg is different from duck egg in some way. That’s called subclass.

When a new class takes on properties and methods of an existing class, is called class inheritance or simply inheritance. The class which inherits from a parent class is also called subclass. Inheritance prevents code repetition by organizing code. We are defining three classes i.e User, dealer, consultant. 

The user is the parent class. Dealer is inheriting everything from User class and then the consultant is inheriting everything from dealer class. So the hierarchy becomes, the dealer is a subclass of user and consultant is a subclass of the dealer.

We will be making use of the following functions:

  1. get_parent_class() gets the name of parent class from class instance.
  2. class_parents() shows a list of parent classes
  3. is_subclass_of() checks if the class of current instance is a subclass of a parent class

Visibility Modifiers

Last crazy example, lol, when chicken is sitting on an egg, thats private. When chicken is not sitting on it, farmer and other chickens can have access to it, that’s protected. When a farmer brings eggs to the market, on the shelf, thats public.

Visibility modifiers are used to control access to objects, properties, and methods. There are three visibility modifiers.

  1. Public accessible from anywhere
  2. Protected accessible from class and subclasses
  3. Private accessible from the class only

Let’s see how to use them: