Object Oriented Concepts
Object Oriented Concepts
Constructor Functions
Constructor Functions are special type of functions which are called automatically whenever an
object is created. So we take full advantage of this behavior, by initializing many things through
constructor functions.
PHP provides a special function called __construct() to define a constructor. We can pass as
many as arguments you like into the constructor function.
Destructor:
Like a constructor function you can define a destructor function using function __destruct(). We
can release all the resources with-in a destructor.
Example for constructor and destructor:
<?php
class Student
{
private $sname;
private $course;
public function __construct($sname, $course)
{
echo "Initializing the objects<br><br>";
$this->sname = $sname;
$this->course = $course;
}
public function __destruct()
{
echo "<br><br><br>Detroying Object";
}
public function showName()
{
echo "Student name is : ". $this->sname;
echo "<br>Course is: ".$this->course;
}
}
$s1 = new Student("Charan","BCA");
$s1->showName();
?>
The output is:
Initializing the objects
Student name is : Charan
Course is: BCA
Detroying Object
The variable $this is a special variable and it refers to the same object ie. itself.
Inheritance:
PHP class definitions can optionally inherit from a parent class definition by using the extends
clause. The syntax is as follows –
class Child extends Parent
{
<body definition>
}
The effect of inheritance is that the child class (or subclass or derived class) has the following
characteristics −
• Automatically has all the member variable declarations of the parent class.
• Automatically has all the same a member function as the parent, which (by default) will
work the same way as those functions do in the parent.
Different types of Inheritance
1) Simple Inheritance
2) Multilevel Inheritance.
3) Hierarchical Inheritance.
Simple Inheritance:
In simple inheritance there will be exactly one super class and one subclass i.e. subclass will get
the functionality from exactly only one super class. It is supported by class data type.
Figure:
<?php
class Box
{
public $width;
public $height;
public $depth;
function __construct($width, $height, $depth)
{
$this->width = $width;
$this->height = $height;
$this->depth = $depth;
}
public function volume ()
{
return $this->width * $this->height * $this->depth;
}
}
class BoxWeight extends Box
{
public $mass;
function __construct($width, $height, $depth, $mass)
{
$this->width = $width;
$this->height = $height;
$this->depth = $depth;
$this->mass = $mass;
}
public function density ()
{
return $this->volume () * $this->mass;
}
}
$boxWeight = new BoxWeight(10, 20, 30, 40);
?>
Multilevel Inheritance:
In multilevel inheritance one super class can have many sub classes. One subclass can have
many indirect super classes i.e. one is direct super class all remaining are indirect super classes.
Figure:
<?php
class Aves
{
public function nature()
{
echo "Generally, Aves fly";
}
}
class Bird extends Aves
{
public function eat()
{
Echo "Eats to live";
}
}
class Parrot extends Bird
{
public function food()
{
echo "Parrot eats seeds and fruits";
}
}
?>
Hierarchical Inheritance:
In this one super class can have many direct sub classes. It is supported by class data type.
Figure:
<?php
class Aves
{
public function nature()
{
echo "Generally, Aves fly";
}
}
class Parrot extends Aves
{
public function eat()
{
echo "Parrot eats fruits and seeds";
}
}
class Vulture extends Aves
{
public function vision()
{
echo "Vulture can see from high altitudes";
}
}
?>
Overloading
In PHP, overloading typically refers to two concepts: method overloading and property
overloading.
Method Overloading: PHP doesn't support method overloading in the same way that language
like Java do, where we can have multiple methods with the same name but different parameters.
However, we can simulate method overloading using variable-length argument lists or optional
arguments.
Property Overloading: PHP supports property overloading through the magic methods __get()
and __set(). These methods allow you to intercept attempts to read or write inaccessible
properties and handle them dynamically.
Here’s an example of how we can implement method overloading in PHP using variable-length
argument lists or optional arguments:
class OverloadingExample
{
public function add()
{
$args = func_get_args();
$num_args = func_num_args();
$sum = 0;
if ($num_args == 2)
{
$sum = $args[0] + $args[1];
}
elseif ($num_args == 3)
{
$sum = $args[0] + $args[1] + $args[2];
}
return $sum;
}
}
In this example, the add() method can accept variable-length argument lists using
func_get_args() and func_num_args(). Depending on the number of arguments passed, it
performs addition accordingly.
Access Specifiers:
Public Members
Unless you specify otherwise, properties and methods of a class are public. That is to say, they
may be accessed in three possible situations −
• From outside the class in which it is declared
• From within the class in which it is declared
• From within another class that implements the class in which it is declared
Private members
By designating a member private, you limit its accessibility to the class in which it is declared.
The private member cannot be referred to from classes that inherit the class in which it is
declared and cannot be accessed from outside the class.
A class member can be made private by using private keyword infront of the member.
Protected members
A protected property or method is accessible in the class in which it is declared, as well as in
classes that extend that class. Protected members are not available outside of those two kinds of
classes. A class member can be made protected by using protected keyword in front of the
member.
The accessibility rules can be summarized by the following table −