Learn Object Oriented Programming in PHP – OOP Tutorial
In this article series, we are going to learn Object-Oriented Programming (OOP) concepts in PHP.
Pre-requisites
- In order to follow this article, you must have some basic knowledge of PHP.
- Your favorite text editor installed.
- You must have one of the web server solution stack installed like XAMPP, WAMP, MAMP or any other.
Most of the modern PHP frameworks use Object Oriented approaches for Application Developments.
What is object-oriented programming?
OOP is a technique to make the program most effective, easy to maintain & similar to our real life. When we use real-life things as objects in programing this technique is called Object-Oriented Programming.
Object
In this concept, basically we use object word for everything. Every real-world entity can be treated as an object. An object has three things:
- Identity
- Data
- Functionality
Object-Oriented Programming: is a technique to solve the problem by breaking the problem into objects. It’s easy to maintain when we consider everything around us as an object in programming. Actually, we break a huge problem into small problems and then solve it easily.
Class
A PHP Class is a group of values with a set of operations to manipulate these values. Classes facilitate modularity and information hiding. Classes are used to define a new data type. Objects are created from a Class. Class is just like a blueprint or description of object. Object is an instance of the Class.
Rules for Class name:
- The class name can be any valid label.
- It can’t be a PHP reserved word.
- Any valid class name starts with a latter or underscores, followed by any number of latter, numbers or underscores.
class Classname { Var-type $variable_name; Var-type $variable_name; function Method_name( ) { Body of method; } function Method_name( parameter_list) { Body of method; } }
In the given above Code, the word class is the keyword when we make a class and Also write a name ( except reserved words ) of this class after written the class keyword. Being a good programmer try to put the first character of the class name is capital anyway It’s not necessary. After this, you put open and close braces. Now you write the whole code in these braces.
Then put var keyword for variable declaration and after this, you write any name for the variable. As you know the end of every statement put a semicolon. Like
var $variable_name; //global variable
When we declare a variable in a class, It’s also called Data member, property or attribute.
Functions
It performs a specific task. Function or method helps to break a big problem into small problems. It has two types of returning and non-returning functions. Its also called method or member function.
Value-Returning Functions: return integer, string or Boolean values after the operation, the parametric values which accept the function.
Non-Returning functions: do not return any value. It accepts parameters and displays or performs operations on it.
The Function may have parameters or not. Parameters are optional. It depends on the type of problem you are working with.
Syntax of Function that takes no parameter
function Method_name() { //Body of method; }
Syntax of Function that takes a parameter
function Method_name(parameter_list) { // Body of method; }
Note: There is no semicolon after the parenthesis. It causes a compilation error.
function Method_name() ; //error { //Body of method; }
Let us take an example in order to understand the class.
As mentioned above, we model the real-world problems in the object-oriented sense.
Let’s assume that as a programmer I have a project for an application that shows the model number of mobile. Different companies come to me for this same problem.
I have one old technique I make function as the name of that company. But as a developer writing the same code again and again for every company is time-consuming.
You first make a class and put the Class name as you like
In this example, we use the class name Mobile
class mobile { }
We need to think about what is properties and functions a mobile ca have.
Example of some properties
- RAM
- Hard
- SD Slot
- Storage
- Screen size
- Camera
All these things are properties or attributes.
These things will make a variable in this class.
Now, we need a model number against every company
Class Mobile { Var- type $model; //global variable }
We make a variable for the mobile model because it’s a property of mobile. Now, we will examine the functionalities.
We know the things which define the operation. When write function is class we use the word “method”.
- Volume up and down.
- Click the camera to capture the picture.
- Flash
- Music
According to example requirement model function will be shown so
We write function which shows the model.
Class Mobile { $model; //global variable function showModel( $number) { global $model; $model = $number; Echo “Model Number: ” .$model; } }
As mention in the above Code, the global keyword is used when we write the global variable in the function. After this, you can access this keyword. The value which accepts as a parameter is assigned in $model variable. The last step of this function is to show the value of the model number.
Three types of properties to declare variables in Class
- Public
- protected
- private
those types change the behavior of the properties so as a quick rundown, the Public allows you to access the property outside the class as well as change its definition to change its value.
Protected allows the property to be accessed within the class as well as within an extending class.
Private means that it’s only accessible internally to that class. It can’t be accessed by extending class nor from outside of the class.
Class Mobile { Public $name; public $model; public $price; }
Constructors:
Now we need to instantiate the class and there’s a magic method that automatically runs during instantiation that is the constructor method and like the properties we need to define the type of method and all method within the class can have these types so again its pipe private-public and protected they have the similar meaning as the properties in the sense that private can only be accessed internally to that class protected can be overwritten and accessed in other classes and public can be accessed from outside of the class.
Now, we write the public function the constructor is the magic method as I mentioned magic function starts with an underscore. For function also need to write keyword ‘function’.
We actually supply some properties into $name ,$model and the last is $price.
After this, now we need to do is set the values of these properties for that we use the keyword ‘this’ and then ‘-’ then we put the greater than symbol ‘>’ & put the variables individually.
Class Mobile { Public $name; public $model; public $price; public function __constructor($name,$model,$price) { $this->name = $name; $this->model = $model; $this->price = $price; } }
Now the thing that we need to do in order to create this car class is we need to instantiate it we need to create an object a member instance that will have some states some explicit values set against this car. The following code is described that above discussion. Note that code written in the outside the curly bracket of the class.
PHP
Class Mobile { Public $name; public $model; public $price; public function __constructor($name,$model,$price) { $this->name = $name; $this->model = $model; $this->price = $price; } Public function whichMobile() { return “Mobile name is” . $this->name . ”Mobile model is ”. $this->model. “Mobile price is ”. $this->price; } } $Iphone = new mobile(‘iphone’,’10’,’$700’); $sumsung = new mobile(‘sumsung’,’note7’,’$700’); echo $Iphone->whichMobile; echo $sumsung->whichMobile;
The function ‘which mobile’ use the above code is to return the all values of the mobile with a description when this method is called.
The last two lines of the above code is to call that function with multiple objects of mobile.
Here $Sumsung and $Iphone are objects.
If we want to get the individual values of name or some other variable then we make individual functions for every variable.
Public function getPrice() { Return $this->price; }
This function give me the value of price you can also make the model and name function according to that function. more details
Here’re some more related Articles: