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

Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
machuga
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
Chris Tankersley
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
Alena Holligan
 
Migration from Procedural to OOP
Migration from Procedural to OOP Migration from Procedural to OOP
Migration from Procedural to OOP
GLC Networks
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOP
Achmad Mardiansyah
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
sannykhopade
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
akankshasorate1
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdf
bhagyashri686896
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
HarshuPawar4
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
switipatel4
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Alena Holligan
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 

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

Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Only oop
Only oopOnly oop
Only oop
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
Migration from Procedural to OOP
Migration from Procedural to OOP Migration from Procedural to OOP
Migration from Procedural to OOP
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOP
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
OOP
OOPOOP
OOP
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 

Recently uploaded

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

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.