OOPs In PHP
OOPs stands for Object Oriented Programming Structure. I have explained to you in my previous article about the fundamentals of objects, classes, and their various features.
Here we will learn OOPS in PHP with an example
Everything we see around us are nothing but objects. We are surrounded by different objects. OOPs is nothing more than molding real life things in the form of programming.
Take an example of a TV. A TV is an object which will have its attributes and own behaviors. Different objects have different attributes and behaviors.
Attributes of Tv: Screen Size, Model, Color.
Behaviors: Volume Up, Volume Down, ON and OFF.
In the same way, if we talk about OOPs in PHP, an object has variables as its attributes and functions as its behaviors.
Steps to use any function
- Define a class
- Create Attributes i.e Variables
- Then add behaviours i.e functions
- Call Functions
Figure: parent_class
Have a look at the above syntax in PHP. Here I have followed all these steps required to use any function.
- I have created a class named ‘TV’(Defined a class). For defining any class a keyword class is used before the class name.
- Second step is to create attributes (variables). Here I have created three attributes: color, volume, and model and set their default values as red, 2, sony respectively. You must be wondering why did I use the keyword public before declaring any variable. I will explain to you all about this in a later part of this article.
- Third step is to add behaviors (functions). For adding any function (i.e behaviors), we have to use a keyword named Function before any function name. Here we have used two functions as volumeUp and volumeDown.
Note: $this refers to a particular object in class. Here the particular object is TV.
- Last step is to call the function that has been created. For calling function, you have to create objects. Here in our syntax, as you are seeing, we have created two objects and saved it in two variables as $tv_one and $tv_two.
Constructor Function
In PHP5, a new function has been included which is called Constructor function (also called as magic function).
Let us see how can we use this Constructor function. Magic functions in PHP are written starting with two underscore symbols(__). Here we don’t need to call the constructor function, whenever a new object is created the constructor function is automatically called.
Figure: construct function
As you are seeing in the above code, I have included another function. This function is called a constructor function and is preceded by double underscore sign(__). Here I have taken 3 parameters and defined as variables i.e $m, $v, $c.
Then assign the values to the variables. After that we have created a new object with all the respective parameters and echoed the value which we want to get as output.
Inheritence
In OOP, Inheritence is the concept where a class of objects is defined, whenever any sub class is defined, it inherits one or many properties of its parent class. This feature of object oriented programming helps in fast development of program.
Figure: Inheritance class
In the above code we have used a child class. In defining a child class we use a keyword extends before the parent class whose child class is to be made. You can then add variables which you have to include. In the case of the above example I have used two child classes ‘tvwithTimer’ & ‘plazmaTv’. Inside that I have created two public variables ‘timer’ &’plazma’ respectively. This is basically done to add extra features to the main class. This helps in code re-usability.Thus, Inheritence is an important concept of OOPS.
Encapsulation
It is just a process to determine the access of variables and member functions. We set access levels for variables and member functions from where we should access it and from where not to access. In OOPS we can access any variables and functions from three places,
- Within the class
- Access over the object
- Child class
There are three access levels,
Getter & Setter Functions
Whenever the access level is set to private, for displaying any sort of data, we have to create a getter and setter function. By doing
this we can just read or echo any information.
Figure: Encapsulation
Getter function is only used to return value. New class would also be created within a class only (for private). In the above code we are seeing that a public keyword is also used beside the function.This public is nothing but just to assign the access level of the function. By default, the access level is public, but writing this will help you to understand the code later on. So you must specify the access level before functions or variables. To echo the information of this private accessed model, we have just called the getter function.
Method Overriding: It is only applicable to derived classes where the parent class has defined a method and the derived class wishes to override that method.
Abstraction
Abstract means incomplete. Abstraction is another important feature of object oriented programming. Here, function is declared but not implemented.
In the following code I have created an abstract class baseEmployee and considered it as a parent class. Here I have included a construct function in the parent class. I have created an abstract class in the parent class by the name
getmonthlysalary(). Abstract class can only be declared, it is not implemented. You can implement it anywhere in the new class created. An abstract class declared should be included at least once in every class. Later on you can include another function and its behavior also.
Figure: Abstraction
Static Variables and Methods
We know that all the functions and variables are related to its objects. But here in the case of static functions and variables they are not.
Static functions and variables are related directly to the class. Here a static keyword is used before any function or variable to make it a static one.
Eg: public static $data; */static variable/*
Public static xyz(); */static function/*
We know that normally the variables and functions are accessed by creating a new object and call the respective function or variable, but here in this case we are not dealing with object.
Eg: abc::xyz(); */calling a function*/
abc::$data
Here $this keyword is not used as it refers to the object. Have a look at the code. You will be more clear with that.
Figure: Static
Here in the place of
$this in function, we make use of self keyword in static. Along with the keyword we use double colon symbol (:: -
Scope Resolution Operator).