Object oriented programming in PHP (PHP OOP)

Object oriented programming in PHP (PHP OOP)

Object oriented programming concept is very powerful concept, which have various advantages for developer like reusing the code structure and data hiding. Here are some concept for object oriented programming in PHP (PHP OOP).

1. Object Oriented

Objects are small entity for the programming. Objects can be said as data type of template called class, which wrap data into object. Using a single object you can perform various operations.

2. Encapsulation

Wrapping of data under one unit is called encapsulation. In simple ways : a shield that protect the data being accessed from outside world. Generally variables or data can not be accessed from other classes. It is achieved by access specified. It also called data hiding concept.

encapsulation can be achieved by declaring the members as private inside class.

3. Class

It is set of properties or methods from which objects are created. those objects are of one type. Below is the example of class in php:

class Car{
  private $type; //class member

  public function startCar(){  //class method
   //do something
  }
}

4. Inheritance

Inheritance is the most powerful concept in any of the object oriented programming language. It allow to inherit the feature of another class. There is concept of superclass, subclass. Superclass is the class which is getting inherited by another class called subclass.

//superclass
class A{
 public $a;

 protected function setA(){
  return $this->a = 10;
 }
}
//subclass
class B extends A{
 public function getA(){
  echo parent::setA();
 }
}
$obj = new B();
$obj->getA();

It will 10 as output. here $obj is the object of B class which inherits the a variable of super class A. using the object we can access the public member of the class B. Class B can access protected and public member of its superclass.

  • Access Specifiers : There are public, protected and private specifiers to be used in the classes. Public members can be accessed anywhere in the class or outside the class. Protected members can be accessed within that class or its inherited class (subclass). Private members only can be accessed only within that class.

5. Abstraction

Data Abstraction is that the property by virtue of which only the essential details are showed the user.The trivial or the non-essentials units aren’t showed the user. Ex: A car is viewed as a car instead of its individual components.
Data Abstraction can also be defined because the process of identifying only the specified characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of comparable type and also help in classifying/grouping the objects.

In real world, lets take example of a Car. A person who driving the car does not know about its internal functionality like how the engine works or how break works, He knows about the outer functionality like processing accelerator, stopping car by brake etc. This is abstract as the person knows only about implementation of those functionality but those are hidden from that person.

Techspook

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment