In the php 5 we can use the object-oriented concepts in the php code, and it will make the coding easier.
Advantages of using oop in php.
Advantages of using oop in php.
- faster and easier to execute.
- provides a clear structure for the programs.
- No need to repeat the code.
- Help to create the reusable applications.
In the php also have classes and objects.
Class – Template for objects.
Objects – is an instance of a class.
We can define the class using class keyword.
class have a pair of curly braces ({}).
All its properties and methods go inside the braces.
<?php
class Pets {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
This class consisting of two properties ($name and $color).
And two methods set_name() and get_name() for setting and getting the $name property.
In here variables are called properties .
And functions are called methods.
Class objects.
Every class contaning the objects and class have one or more than one objects.
Member Variable − These are the variables define as inside a class.
Function of member − These are the function defined inside a class and are using to access object data.
Php Inheritance − When a class is defined by inheriting existing function of a parent class then it is calling inheritance.
Super class − A class that is inheritting from by another class.
Also, we can call this base class or super class.
Child Class − A class that inherits from another class also we can call this as subclass or derived class.
Polymorphism − This is an object-oriented concept where same functions can use for different purposes.
Data Abstraction − Any representation of data in which the implementation details with hiding it.
Encapsulation − refers to a concept where we encapsulate all
the data and member functions together to form an object.
Constructor of the code − Refers to a special type of function which will call automatically.
Destructor − refers to a special type of function.