SlideShare a Scribd company logo
Object-Oriented Programming
(OOP)
MD. ATIKUR RAHMAN
PHP
Lecture - 04
Programmer, Silkcity Solution
Trainer, CBA IT
2
Agenda
o Important points to remember while using inheritance
o Overriding Inherited Methods
o Copy Constructor
o Encapsulation
o Abstraction
o Abstraction Vs. Encapsulation
Important points to remember
while using inheritance
o Child class can only access and utilize non-private
parent-class properties and methods.
o Child class can also have its own methods that the
parent class cannot find or access.
o Child class can override and implement a method
specified in parent class.
Overriding
Inherited Methods
o Methods overriding, both parent and child classes should
have same method name with and number of arguments.
o Used to replace parent method in child class.
o The purpose of overriding is to change the behavior of parent
class method.
o The two methods with the same name and same parameter is
called overriding.
5
Overriding Inherited Methods
Example
<?php
class P {
public function geeks() {
echo "Parent<br/>";
}
}
class C extends P {
public function geeks() {
echo "Child";
}
}
$p = new P();
$c = new C();
$p->geeks();
$c->geeks();
?>
OUTPUT
Parent
Child
6
Copy Constructor is a type of constructor which
is used to create a copy of an already existing
object of a class.
Copy
Constructor
7
Copy Constructor
Example
<?php
class CopyConstructor {
public $name;
public function __construct() {
}
public function copyCon(CopyConstructor $object){
$this->name = $object->name;
}
public function show(){
echo "Name = " . $this->name . "<br/>";
}
}
$obj1 = new CopyConstructor();
$obj1->name = 'Copy Constructor';
$obj1->show();
echo '<br/>';
$obj2 = new CopyConstructor();
$obj2->copyCon($obj1);
$obj2->show();
?>
8
Encapsulation is a process of binding the
properties and methods together in a single unit
called class.
Encapsulation
o Declare each property private.
o Create public set method for each property to set the
values of properties.
o Create public get method for each property to get the
values of properties.
Data Encapsulation steps
9
Encapsulation
Example
<?php
class person
{
private $Name, $Age;
public function setNameAge($name, $age) {
$this->Name = $name;
$this->Age = $age;
}
public function displayNameAge() {
echo "Name : ".$this->Name."<br/>";
echo "Age : ".$this->Age."<br/>";
}
};
$pObject = new person();
$pObject->setNameAge("Jhon Luther", 40);
$pObject->displayNameAge();
?>
OUTPUT
Name : Jhon Luther
Age : 40
10
Abstraction is the concept of object-oriented
programming that “shows” only essential attributes
and “hides” unnecessary information.
The main purpose of abstraction is hiding the
unnecessary details from the users.
Abstraction
abstract class Class_Name {
//class code
}
abstract access_modifier function function_name();
abstract class abstract method
11
Abstraction
Rules
o abstract keyword is used to declare an abstract class or method.
o An abstract class must contains at least one abstract method. However, it can also contains
non-abstract methods as well.
o An abstract method has no body. (It has no statements.) It declares an access modifier,
return type, and method signature followed by a semicolon.
o Objects cannot be created from abstract classes.
o If the abstract class uses type hinting (type declaration for return values), the child class
should use the same.
public function myMethod3() : int {...}
o The child class should override (redeclare) all the abstract methods.
o The arguments for methods should be the same as the abstract method.
o The child class can have arguments with default values where the abstract class hasn't
defined.
public function myMethod($name, $age, $country = 'USA') {...}
o The visibility of the child's method should be the same as the parent's or less restricted.
12
Abstraction
Example
<?php
abstract class Person
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
abstract public function greet(): string;
}
?>
Explanation: In the parent class, the __construct method and $name property are declared. So, the
child class will automatically have them. But, greet() is a method that should be defined in all the child
classes and they should return a string.
13
Abstraction
Example (Child Classes)
<?php
class Programmer extends Person {
public function greet(): string {
return "Hello World from ". $this->name;
}
}
class Student extends Person {
public function greet(): string {
return "Howdy! I'm ". $this->name;
}
}
class Teacher extends Person {
public function greet(): string {
return "Good morning dear students";
}
}
$programmer = new Programmer('John');
echo $programmer->greet();
$student = new Student('Doe');
echo $student->greet();
$teacher = new Teacher('Mary');
echo $teacher->greet();
?>
Abstraction Vs. Encapsulation
14
Parameter Abstraction Encapsulation
Use for
Abstraction solves the problem and issues that
arise at the design stage.
Encapsulation solves the problem and issue that
arise at the implementation stage.
Focus
Abstraction allows you to focus on what the
object does instead of how it does it
Encapsulation enables you to hide the code and data
into a single unit to secure the data from the outside
world.
Implementation
You can use abstraction using Interface and
Abstract Class.
You can implement encapsulation using Access
Modifiers (Public, Protected & Private.)
Focuses Focus mainly on what should be done. Focus primarily on how it should be done.
Application During design level. During the Implementation level.
Thank you

More Related Content

Similar to PHP OOP Lecture - 04.pptx (20)

PPTX
Ch8(oop)
Chhom Karath
 
PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PDF
PHP OOP
Oscar Merida
 
PDF
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
PDF
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
PDF
OOP in PHP
Alena Holligan
 
DOCX
Benefits of encapsulation
Muhammad Nawzir Khan
 
PPTX
Oo ps
seema chauhan
 
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
PPTX
Intro to OOP PHP and Github
Jo Erik San Jose
 
PPTX
Lecture 2
talha ijaz
 
PDF
Programming Laboratory Unit 1.pdf
swapnilslide2019
 
PPTX
OOP in PHP.pptx
switipatel4
 
PDF
Web Design & Development - Session 9
Shahrzad Peyman
 
PPTX
Php oop (1)
Sudip Simkhada
 
PDF
Take the Plunge with OOP from #pnwphp
Alena Holligan
 
PDF
Intro Oop
Kamatchi Selvam
 
ODP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PPTX
Object oriented programming in php
Aashiq Kuchey
 
PPTX
Object Oriented Programming Concepts for beginners
Vibhawa Nirmal
 
Ch8(oop)
Chhom Karath
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PHP OOP
Oscar Merida
 
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
OOP in PHP
Alena Holligan
 
Benefits of encapsulation
Muhammad Nawzir Khan
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
Intro to OOP PHP and Github
Jo Erik San Jose
 
Lecture 2
talha ijaz
 
Programming Laboratory Unit 1.pdf
swapnilslide2019
 
OOP in PHP.pptx
switipatel4
 
Web Design & Development - Session 9
Shahrzad Peyman
 
Php oop (1)
Sudip Simkhada
 
Take the Plunge with OOP from #pnwphp
Alena Holligan
 
Intro Oop
Kamatchi Selvam
 
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Object oriented programming in php
Aashiq Kuchey
 
Object Oriented Programming Concepts for beginners
Vibhawa Nirmal
 

Recently uploaded (20)

PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Ad

PHP OOP Lecture - 04.pptx

  • 1. Object-Oriented Programming (OOP) MD. ATIKUR RAHMAN PHP Lecture - 04 Programmer, Silkcity Solution Trainer, CBA IT
  • 2. 2 Agenda o Important points to remember while using inheritance o Overriding Inherited Methods o Copy Constructor o Encapsulation o Abstraction o Abstraction Vs. Encapsulation
  • 3. Important points to remember while using inheritance o Child class can only access and utilize non-private parent-class properties and methods. o Child class can also have its own methods that the parent class cannot find or access. o Child class can override and implement a method specified in parent class.
  • 4. Overriding Inherited Methods o Methods overriding, both parent and child classes should have same method name with and number of arguments. o Used to replace parent method in child class. o The purpose of overriding is to change the behavior of parent class method. o The two methods with the same name and same parameter is called overriding.
  • 5. 5 Overriding Inherited Methods Example <?php class P { public function geeks() { echo "Parent<br/>"; } } class C extends P { public function geeks() { echo "Child"; } } $p = new P(); $c = new C(); $p->geeks(); $c->geeks(); ?> OUTPUT Parent Child
  • 6. 6 Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class. Copy Constructor
  • 7. 7 Copy Constructor Example <?php class CopyConstructor { public $name; public function __construct() { } public function copyCon(CopyConstructor $object){ $this->name = $object->name; } public function show(){ echo "Name = " . $this->name . "<br/>"; } } $obj1 = new CopyConstructor(); $obj1->name = 'Copy Constructor'; $obj1->show(); echo '<br/>'; $obj2 = new CopyConstructor(); $obj2->copyCon($obj1); $obj2->show(); ?>
  • 8. 8 Encapsulation is a process of binding the properties and methods together in a single unit called class. Encapsulation o Declare each property private. o Create public set method for each property to set the values of properties. o Create public get method for each property to get the values of properties. Data Encapsulation steps
  • 9. 9 Encapsulation Example <?php class person { private $Name, $Age; public function setNameAge($name, $age) { $this->Name = $name; $this->Age = $age; } public function displayNameAge() { echo "Name : ".$this->Name."<br/>"; echo "Age : ".$this->Age."<br/>"; } }; $pObject = new person(); $pObject->setNameAge("Jhon Luther", 40); $pObject->displayNameAge(); ?> OUTPUT Name : Jhon Luther Age : 40
  • 10. 10 Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction abstract class Class_Name { //class code } abstract access_modifier function function_name(); abstract class abstract method
  • 11. 11 Abstraction Rules o abstract keyword is used to declare an abstract class or method. o An abstract class must contains at least one abstract method. However, it can also contains non-abstract methods as well. o An abstract method has no body. (It has no statements.) It declares an access modifier, return type, and method signature followed by a semicolon. o Objects cannot be created from abstract classes. o If the abstract class uses type hinting (type declaration for return values), the child class should use the same. public function myMethod3() : int {...} o The child class should override (redeclare) all the abstract methods. o The arguments for methods should be the same as the abstract method. o The child class can have arguments with default values where the abstract class hasn't defined. public function myMethod($name, $age, $country = 'USA') {...} o The visibility of the child's method should be the same as the parent's or less restricted.
  • 12. 12 Abstraction Example <?php abstract class Person { public $name; public function __construct($name) { $this->name = $name; } abstract public function greet(): string; } ?> Explanation: In the parent class, the __construct method and $name property are declared. So, the child class will automatically have them. But, greet() is a method that should be defined in all the child classes and they should return a string.
  • 13. 13 Abstraction Example (Child Classes) <?php class Programmer extends Person { public function greet(): string { return "Hello World from ". $this->name; } } class Student extends Person { public function greet(): string { return "Howdy! I'm ". $this->name; } } class Teacher extends Person { public function greet(): string { return "Good morning dear students"; } } $programmer = new Programmer('John'); echo $programmer->greet(); $student = new Student('Doe'); echo $student->greet(); $teacher = new Teacher('Mary'); echo $teacher->greet(); ?>
  • 14. Abstraction Vs. Encapsulation 14 Parameter Abstraction Encapsulation Use for Abstraction solves the problem and issues that arise at the design stage. Encapsulation solves the problem and issue that arise at the implementation stage. Focus Abstraction allows you to focus on what the object does instead of how it does it Encapsulation enables you to hide the code and data into a single unit to secure the data from the outside world. Implementation You can use abstraction using Interface and Abstract Class. You can implement encapsulation using Access Modifiers (Public, Protected & Private.) Focuses Focus mainly on what should be done. Focus primarily on how it should be done. Application During design level. During the Implementation level.