Object Oriented Programming in PHP

Hi there, today I am going to explain about Object Oriented Programming in PHP with syntax and few examples.

The Object Oriented Programming Concepts are:
  1. Class
  2. Objects
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction  
1.Class:
  1. Class is a blueprint of an object.Which includes local methods and local variables
  2. The class can be declared using "Class" Keyword and followed by the name of the class and a set of curly braces ({}).
Syntax to create Class in PHP:
Let's create the class in the name of "Maths".
<?php
class Maths{
    // Class properties and methods goes here
}

?>

2.Objects:
Object is an instance of a class.
When Class is created, we can create any number of objects in that Class.
In the below example, new keyword which is used to instantiate an object. Here $my_obj represents an object of the class Maths
Creating an Object:
<?php
$my_obj = new Maths();
?>  
Calling a member Function:
When an object is created, we can access variables and function of the class with the help of "->" operator.
<?php
$my_obj->add(10, 12); 
 ?>
Example:
<?php
class Maths{
    public function add($a,$b){

        echo "Addition of two numbers:".$a+$b;
    }
}

$my_obj = new Home();
$my_obj->add(10, 12);
?>
Output:
Addition of two numbers:22

Access Specifiers:
To set the access rights for the class methods and variables we use access specifiers.
Public: When we define class members as public, It can be accessed from anywhere, even from outside the class
Private: When we define class members as private, It can be accessed from within the class itself.
Protected: This is same as private, with one exception, the class members defined as protected can still be accessed from its subclass.

When to use Access Specifiers:
Access Specifierclassesfunctionsvariables
publicNot ApplicableApplicableApplicable
privateNot ApplicableApplicableApplicable
protectedNot ApplicableApplicableApplicable

3.Encapsulation:
  1. Which is used hide internal implementation details of an object.
  2. Visibility is the mechanism for encapsulation.
  3. For the security reason, we can make private method. Private method means it can be accessed within same class. Class can't access private method of other class.That's why encapsulation is known as data hiding is the main advantage for encapsulation
  4. Second advantage of encapsulation is you can make the class read only or write only by providing setter or getter method.
So finally the concept of Encapsulation in PHP is hiding internal information of object to protect from the other object.
<?php
class Person {
    private $name;

    public function setName($name) {
        $this->name = $name;
    }

    public function getName($name) {
        return $this->name;
    }
}

$robin = new Person();
$robin->setName('SleepyCoder');
$robin->getName();

?>
In this simple class above. Field $name is encapsulated (private). Users of the class is not aware how $name is stored in Person class. Right now the $name is stored in memory. We can modify internal code of Person class to store it to a flat file or event a database. Users of the class will not need to change any code, in fact they do not even know how $name is stored, because that is encapsulated and hided from them.

4.Inheritance:
  1. When the properties and the methods of the parent class are accessed by the child class, we call the concept has inheritance.
  2. Parent class decides what and how its properties/methods to be inherited by declared visibility.  
<?php
class Shape {
    public function name() {
        echo "I am a shape";
    }
}
class Circle extends Shape {

}
$circle = new Circle();
$circle->name(); // I am a shape 


5.Polymorphism:
  1. Which is having a single form but many different implementation ways. The main purpose of polymorphism is;  
  2. Simplify maintaining applications and making them more extendable.
  3. Basically it means PHP is able to process objects differently depending on their data type or class. This powerful feature allows you to write interchangeable objects that sharing the same interface.
6.Abstraction:
In OOP, abstraction is a concept in which a class has methods without implementation. The idea is to have a template and let the child class that inherits the parent class implement the method.
How to create an abstract method?
  1. If a class has an abstract method then we also add the abstract keyword before the class. 
  2. We use the keyword abstract to create abstract method .
  3. We can't instantiate an abstract class
Example:
<?php
abstract class Sample {
    //abstract method
    public abstract function foo();

?> 
Inherit an abstract class
  1. When an abstract class is inherited by the child class, the abstract method of the parent class must be implemented in the child class.
  2. If the child class is not implementing an abstract method of the parent class then the child class must be declared abstract. 


Comments

Post a Comment