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

PDF
Demystifying Object-Oriented Programming #phpbnl18
PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PPTX
PHP OOP Lecture - 02.pptx
ZIP
Object Oriented PHP5
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - Lone Star PHP
PHP OOP Lecture - 02.pptx
Object Oriented PHP5
UNIT III (8).pptx
UNIT III (8).pptx
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College

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

PPTX
Ch8(oop)
PDF
Objects, Testing, and Responsibility
PPTX
Only oop
PPTX
Coming to Terms with OOP In Drupal - php[world] 2016
PDF
Demystifying oop
PDF
Migration from Procedural to OOP
PPT
Class 7 - PHP Object Oriented Programming
PDF
OOP in PHP
PDF
PHPID online Learning #6 Migration from procedural to OOP
PDF
Object Oriented Programming in PHP
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision (3).pdf
PPTX
OOP in PHP.pptx
PDF
OOP in PHP
PPT
PPT
Advanced php
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
PPT
Php object orientation and classes
Ch8(oop)
Objects, Testing, and Responsibility
Only oop
Coming to Terms with OOP In Drupal - php[world] 2016
Demystifying oop
Migration from Procedural to OOP
Class 7 - PHP Object Oriented Programming
OOP in PHP
PHPID online Learning #6 Migration from procedural to OOP
Object Oriented Programming in PHP
php_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 (3).pdf
OOP in PHP.pptx
OOP in PHP
Advanced php
Demystifying Object-Oriented Programming - PHP[tek] 2017
Php object orientation and classes
Ad

Recently uploaded (20)

PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Weekly quiz Compilation Jan -July 25.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A systematic review of self-coping strategies used by university students to ...
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
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.