OOP in PHP
OOP in PHP
OOP in PHP
3. OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
4. OOP makes it possible to create full reusable applications with less
inherit all the properties and behaviors from the class, but each
object will have different values for the properties.
• A class is a collection of
objects.
• Object has properties and
behavior
• Characteristics of class are
properties of object
• Behavior of class is action
associated with it called
method
Define a class
A class is defined by using <?php
the class keyword, followed by the class Fruit
name of the class and a pair of { // Properties
curly braces ({}). public $name;
public $color;
All its properties and methods go // Methods
inside the braces: function set_name($name)
Syntax {
<?php $this->name = $name;
class classname }
{ function get_name()
{
// code goes here...
return $this->name;
} }
?> }
?>
Define an Object
Classes are nothing without objects! We <?php
can create multiple objects from a class. class Fruit
Each object has all the properties and { // Properties
methods defined in the class, but they will public $name;
have different property values. public $color;
// Methods
Objects of a class is created using function
the new keyword. set_name($name)
$apple = new Fruit(); {
$banana = new Fruit(); $this->name = $name;
$apple->set_name('Apple'); }
$banana->set_name('Banana'); function get_name()
echo $apple->get_name(); {
return $this->name;
echo "<br>";
}
echo $banana->get_name(); }
?> Apple Banana
Define an Object & Property visibility
//Class example class Myclass
<?php {
class Car public $p1;
{ private $p2;
//Nothing to see here; protected $p3;
} }
$maruti=new Car(); Public can be accessed by any code,
$honda=new Car(); whether code is inside the class or outside
print_r($maruti); class.
print_r($honda); Private can be accessed by only by code
?> inside the class, only methods in the class
Car Object ( ) Car Object ( ) can be access its contents.
Protected can be accessed within the
class and by classes derived from that
class.
Sample program
<?php $stud1=new Student();
class Student $stud2=new Student();
{ //Properties
public $name; $stud1->set_name("Xyz");
public $city; $stud2->set_name("ABC");
//Methods
function set_name($name) echo $stud1->get_name();
{ echo $stud2->get_name();
$this->name=$name; ?>
} The $this keyword refers to the
function get_name() current object, and is only
{ available inside methods.
1. Inside the class (by adding a
return $this->name;
set_name() method and use
}
$this):
}
2. Outside the class (by directly
no implementation.
Class can inherit from one class but class can implement 1 or more
interfaces.
<?php
interface InterfaceName
{
public function someMethod1();
public function someMethod2($name, $color);
public function someMethod3() : string;
}
?>
PHP Interface example1
<?php $fruit = new apple();
interface fruit $fruit->color();
{ ?>
public function color(); OUTPUT: RED
}
class apple implements fruit
{
public function color()
{
echo “RED";
}
}
PHP Interface example2
<?php class C extends A implements B
//Class A i.e. Parent A {
class A public function display2()
{ {
public function display1() echo "Parent B<br>";
{ }
echo "Parent A<br>"; public function display3()
} {
} echo "Child C";
//Interface B i.e. Parent B }
interface B }
{ $obj=new C();
public function display2(); $obj->display1();
} $obj->display2();
Parent A Parent B Child C $obj->display3();
?>
PHP Access Modifier
Properties and methods can have <?php
access modifiers which control class Fruit
where they can be accessed. {
public $name;
There are three access modifiers:
protected $color;
public - the property or method
private $weight;
can be accessed from everywhere. }
This is default
protected - the property or
$mango = new Fruit();
method can be accessed within $mango->name = 'Mango'; // OK
the class and by classes derived $mango->color = 'Yellow'; // ERROR
from that class $mango->weight = '300'; // ERROR
private - the property or method ?>
can ONLY be accessed within the
class
PHP Access Modifier
<?php private function set_weight($n
class Fruit )
{ { // a private function
public $name; $this->weight = $n;
public $color; }
public $weight; }