OOPs Concepts – PHP

OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming.

Before we go in details, let’s define important terms related to Object Oriented Programming.

  • Class− Simply Class is a collection of member variables and its functions.
  • Object−You define a class once and then make many objects that belong to it. Objects have state behavior and identity.
  • Member functions and Variables− these are the functions and variables defined inside a class and are used to access object data.
  • Inheritance– Simply creating a new class from the existing class. When a class is defined by inheriting existing function of a parent class then it is called inheritance.
  • Polymorphism– Using this you can create multiple functions with their same name.  Or simply saying function name will remain same but it makes take different number of arguments and can do different task.
  • Overloading− a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
  • Data Abstraction− Any representation of data in which the implementation details are hidden (abstracted).
  • Encapsulation− refers to a concept where we encapsulate all the data and member functions together to form an object.
  • Constructor– A Constructor is the same name of its class, will automatic invoke once the object of class in created.
  • Destructor– It refer to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

Example:

<?php

// creating class called myClass

class myClass

{

public $i;

// creating function called myFn

function  myFn()

{

$c=10;

echo “Value is “.$c;

}

}

// Creating object for the class myClass

$ob=new myClass();

// Calling function through class object $ob

$ob->myFn();

?>

Tagged , , , , , , , , , , , , , , , , , , , , , , ,