Object Oriented
Programming, or OOP, refers to the method of programming that invokes
the use of classes to organize the data and structure of an
application. Mostly all php Framework based on Object Oriented Programming.
-
Code Maintainability
-
Code Re-usability
-
Abstraction
-
Molecularity
What are the must learn Object Oriented Programming concepts in PHP?
- Class − This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.example :
/ / my code
}
-
Object − An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.For example:
$object = new car();
-
Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.For example:
class Parent {
// The parent’s class code
}
class Child extends Parent
{
{
// The child can use the parent's class code
}
-
Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.For example:
class Circle implements Shape {
private $radius;
public function __construct($radius)
{
$this -> radius = $radius;
}
// calcArea calculates the area of circles
public function calcArea()
{
return $this -> radius * $this -> radius * pi();
}
}
-
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.For example:
class Foo {
function myFoo() {
return "Foo";
}
}
class Bar extends Foo {
function myFoo() {
return "Bar";
}
}
-
Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).For exampleabstract class Mobile
{
public Calling(); // calling function
public SendSMS(); // calling function
} -
Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.
-
Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.
class Bird
{
public $bird_name = "No any birds for now";
public function __construct($bird_name)
$this->bird_name = $bird_name;
}
}
$bird = new Bird("Sparrow is bird"); // now constructor is called automatically because we have initialized the object or class Bird.
echo $bird->bird_name;
Output of above code is :
Sparrow is bird -
Destructor − refers to a special type of function
which will be called automatically whenever an object is deleted or
goes out of scope.