SlideShare a Scribd company logo
Object Oriented Programming
           in PHP
OOP in PHP
These slides
OOP in PHP
Constructor
                                                Interface

 Encapsulation                         Static




       Destructor
                    Object                           Abstraction

             Inheritance                        Class
Visibility
                               Final    Polymorphism

                     Pattern
What is OOP?
It is a method of programming based on a hierarchy of
classes, and well-defined and cooperating objects.
Why OOP?
  Offers powerful model for writing computer software.

  Easy to partition the work in a project based on objects.

  Object-oriented systems can be easily upgraded from small to large
   scale.

  Reduces software maintenance and developing costs.

  Accept the changes in user requirements or later developments.

  help to develop high quality software easily
Why OOP?

     Modularization

     Abstraction – Understandability

     Encapsulation -- Information Hiding

     Composability -- Structured Design

     Hierarchy

     Continuity
Why OOP?

              Modularization




    Decompose problem into smaller sub-problems
           that can be solved separately
Why OOP?
             Abstraction -- Understandability




 Terminology of the problem domain is reflected in the software solution.
       Individual modules are understandable by human readers
Why OOP?

       Encapsulation -- Information Hiding




  Hide complexity from the user of a software. Protect low-level
                        functionality.
Why OOP?

      Composability -- Structured Design




    Interfaces allow to freely combine modules to produce
                          new systems.
Why OOP?

                   Hierarchy




    Incremental development from small and simple to
                 more complex modules.
Why OOP?

                     Continuity




    Changes and maintenance in only a few modules does
                not affect the architecture.
Main OOP Language Features
  Classes (Modularization, structure)
  Inheritance / extends (Hierarchy of modules, incremental
    development
  Encapsulation ( Public , Private, Protected)
  Composability ( Interfaces / Abstraction )
  Polymorphism ( Hierarchy of modules, incremental
development)
Object in real world
Object
  Object has own properties and actions

  Object can communicate with other object
OOP Features in PHP
  Class Constants
  Autoloading
  Constructor and Destructors
  Visibility
  Inheritance
  Static keyword
  Class Abstraction
  Object Interfaces
  Overloading
  Object Iteration
  Magic Methods
 Object Cloning
  Type Hinting
  Object Serialization
Declaration of Class
PHP 4 :
   class Person
   {
       var $name = ‘default name’;
       function Person()
       {
           //constructor
       }
   }
PHP 5 :
    class Person
    {
        public $name = ‘default name’;
        //automatically calling __construct() magic function
    }
Declaration of Class
PHP 4 :
   class Person
   {                                 Should not a PHP reserved
       var $name = ‘default name’;    word
       function Person()             Starts with a letter or
                                      underscore, followed by
       {                              any number of letters,
           //constructor              numbers, or underscores
       }
   }
PHP 5 :
    class Person
    {
        public $name = ‘default name’;
        //automatically calling __construct() magic function
    }
Object in PHP
<?php
   class Person {
       public $name;
       function setName ($personName) {
           $this->name = $personName;
       }
       function getName ( ) {
           return $this->name;
       }
   }
   //Creating A Object
   $emran = new Person();
   $emran->setName(‘Emran Hasan’);

   echo $emran->getName();
   //Output : Emran Hasan
Class
    Class is a description of an object
    A user-defined data type that contains the variables, properties
      and methods in it.
    Class never executes
    It’s possible to create unlimited no of instance from a class.

Object
  Object is an instance of a class.
  It’s an executable copy of a class
Using __construct magic function
 class Person {
     public $name ;
     function __construct ($personName) {
         $this->name = $personName;
     }
     function getName ( ) {
         return $this->name;
     }
 }
 $emran = new Person(‘Emran Hasan’);
 $alamgir = new Person(‘Alamgir Hossain’);

 echo $emran->getName(); // will print Emran Hasan
 echo $alamgir ->getName(); // will print Alamgir Hossain
Object Assignment in PHP

$emran = new Person(‘Emran Hasan’);
$myfriend = $emran;

$emran->setName( ‘Md Emran Hasan’);

echo $myfriend ->getName();
//Output : Md Emran Hasan
Inheritance

  It’s an object-oriented concept that helps objects to
    work.
  It defines relationships among classes

  Inheritance means that the language gives the ability to
extend or enhance existing objects.
Simple Class Inheritance
class Student extends Person {

}

$newStudent = new Student (‘Hasin Hyder’);

echo $newStudent->getName();
//Output will be : Hasin Hyder
Simple Class Inheritance
class Student extends Person {
    protected $roll;
    function setRoll( $rollNo) {
        $this->roll = $rollNo;
    }
    function getRoll( ) {
        return $this->roll;
    }
}
$newStudent = new Student (‘Hasin Hyder’);
$newStudent ->setRoll( ‘98023’);

echo $newStudent->getName();
echo “Roll no : ” . $newStudent->getRoll () ;
//Output will be : Hasin Hyder , Roll no : 98023
Protecting Data With Visibility
   Public (default) - the variable can be accessed and changed
     globally by anything.

   Protected - the variable can be accessed and changed only by
     direct descendants (those who inherit it using the extends
     statement) of the class and class its self

   Private - the variable can be accessed and changed only from
     within the class
Visibility
class MyClass
{
   public $public = 'Public';
   protected $protected = 'Protected';
   private $private = 'Private';

    function printHello()
    {
      echo $this->public;
      echo $this->protected;
      echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Public
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
Visibility
class MyClass2 extends MyClass
{
   // We can redeclare the public and protected method, but not private
   protected $protected = 'Protected2';

    function printHello()
    {
      echo $this->public;
      echo $this->protected;
      echo $this->private;
    }
}

$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
Abstract
<?php
abstract class Weapon
{
  private $serialNumber;
  abstract public function fire();

     public function __construct($serialNumber)
     {
       $this->serialNumber = $serialNumber;
     }

     public function getSerialNumber()
     {
       return $this->serialNumber;
     }
}
?>
Abstraction
<?php

class Gun extends Weapon         $newGun = new Gun(‘AK-47’);
{                                $newGun->fire();
   public function fire() {      $newGun->getSerialNumber();
      echo “gun fired”;
   }                             $canon = new Canon(‘CAN-5466’);
}                                $canon->fire();
                                 $canon->getSerialNumber();
class Cannon extends Weapon
{
   public function fire() {
      echo “canon fired”;
   }
}

?>
Interface
interface Crud
{
   public function get() {}
   public function insert() {}
   public function update() { }
   public function delete() { }
}
Interface
Class Product implements Crud   Class Customer implements Crud
{                                 public function get() {
  public function get() {             //here goes the code
      //here goes the code        }
  }                               public function insert() {
  public function insert() {         //here goes the code
     //here goes the code         }
  }                               public function update() {
  public function update() {         //here goes the code
     //here goes the code         }
  }                               public function delete() {
  public function delete() {         //here goes the code
     //here goes the code         }
  }                             }
}
More on OOP
  Polymorphism

  Coupling

  Design Patterns
Questions?



Thanks

Tarek Mahmud Apu
Founder, Wneeds
apu.eee@wneeds.com
Reference

  Everything about PHP
    http://php.net

  Object Oriented Programming
    http://www.oop.esmartkid.com/index.htm

  These slides
     http://www.apueee.com

More Related Content

PDF
Architecture logicielle #3 : object oriented design
Jean Michel
 
ZIP
Object Oriented PHP5
Jason Austin
 
PPT
Php Oop
mussawir20
 
PDF
7 rules of simple and maintainable code
Geshan Manandhar
 
PDF
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
PDF
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
PPT
Php object orientation and classes
Kumar
 
PPT
OOP
thinkphp
 
Architecture logicielle #3 : object oriented design
Jean Michel
 
Object Oriented PHP5
Jason Austin
 
Php Oop
mussawir20
 
7 rules of simple and maintainable code
Geshan Manandhar
 
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
Php object orientation and classes
Kumar
 

What's hot (20)

PPTX
Clean Code: Chapter 3 Function
Kent Huang
 
PDF
OOP in PHP
Alena Holligan
 
PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PDF
PHPID online Learning #6 Migration from procedural to OOP
Achmad Mardiansyah
 
PDF
Design patterns in PHP
Jason Straughan
 
PPTX
Oop in-php
Rajesh S
 
PDF
Functions in PHP
Vineet Kumar Saini
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
PDF
Developing SOLID Code
Mark Niebergall
 
PDF
Dependency Injection Smells
Matthias Noback
 
KEY
Clean code and Code Smells
Mario Sangiorgio
 
KEY
Module Magic
James Gray
 
PPT
Oops in PHP
Mindfire Solutions
 
PDF
Advanced PHP Simplified - Sunshine PHP 2018
Mark Niebergall
 
PPT
Class and Objects in PHP
Ramasubbu .P
 
PPTX
Object Oriented Programming Basics with PHP
Daniel Kline
 
PDF
Intermediate OOP in PHP
David Stockton
 
PPTX
Introduction to PHP OOP
fakhrul hasan
 
PPT
PHP- Introduction to Object Oriented PHP
Vibrant Technologies & Computers
 
Clean Code: Chapter 3 Function
Kent Huang
 
OOP in PHP
Alena Holligan
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PHPID online Learning #6 Migration from procedural to OOP
Achmad Mardiansyah
 
Design patterns in PHP
Jason Straughan
 
Oop in-php
Rajesh S
 
Functions in PHP
Vineet Kumar Saini
 
Object Oriented Programming in PHP
Lorna Mitchell
 
Developing SOLID Code
Mark Niebergall
 
Dependency Injection Smells
Matthias Noback
 
Clean code and Code Smells
Mario Sangiorgio
 
Module Magic
James Gray
 
Oops in PHP
Mindfire Solutions
 
Advanced PHP Simplified - Sunshine PHP 2018
Mark Niebergall
 
Class and Objects in PHP
Ramasubbu .P
 
Object Oriented Programming Basics with PHP
Daniel Kline
 
Intermediate OOP in PHP
David Stockton
 
Introduction to PHP OOP
fakhrul hasan
 
PHP- Introduction to Object Oriented PHP
Vibrant Technologies & Computers
 
Ad

Similar to OOP in PHP (20)

PPTX
Oopsinphp
NithyaNithyav
 
PPTX
Ch8(oop)
Chhom Karath
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
Php oop (1)
Sudip Simkhada
 
PPTX
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
PDF
Object Oriented PHP - PART-1
Jalpesh Vasa
 
PPT
Advanced php
hamfu
 
PPTX
Only oop
anitarooge
 
PPTX
Object oriented programming in php
Aashiq Kuchey
 
PDF
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
PDF
Object Oriented Programming in PHP
wahidullah mudaser
 
PDF
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
PPT
Introduction to OOP with PHP
Michael Peacock
 
PDF
09 Object Oriented Programming in PHP #burningkeyboards
Denis Ristic
 
DOCX
Oops concept in php
selvabalaji k
 
PDF
OOPs Concept
Mohammad Yousuf
 
PDF
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Oopsinphp
NithyaNithyav
 
Ch8(oop)
Chhom Karath
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Php oop (1)
Sudip Simkhada
 
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Advanced php
hamfu
 
Only oop
anitarooge
 
Object oriented programming in php
Aashiq Kuchey
 
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Object Oriented Programming in PHP
wahidullah mudaser
 
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
Introduction to OOP with PHP
Michael Peacock
 
09 Object Oriented Programming in PHP #burningkeyboards
Denis Ristic
 
Oops concept in php
selvabalaji k
 
OOPs Concept
Mohammad Yousuf
 
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Ad

Recently uploaded (20)

PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 

OOP in PHP

  • 5. Constructor Interface Encapsulation Static Destructor Object Abstraction Inheritance Class Visibility Final Polymorphism Pattern
  • 6. What is OOP? It is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
  • 7. Why OOP?   Offers powerful model for writing computer software.   Easy to partition the work in a project based on objects.   Object-oriented systems can be easily upgraded from small to large scale.   Reduces software maintenance and developing costs.   Accept the changes in user requirements or later developments.   help to develop high quality software easily
  • 8. Why OOP?   Modularization   Abstraction – Understandability   Encapsulation -- Information Hiding   Composability -- Structured Design   Hierarchy   Continuity
  • 9. Why OOP? Modularization Decompose problem into smaller sub-problems that can be solved separately
  • 10. Why OOP? Abstraction -- Understandability Terminology of the problem domain is reflected in the software solution. Individual modules are understandable by human readers
  • 11. Why OOP? Encapsulation -- Information Hiding Hide complexity from the user of a software. Protect low-level functionality.
  • 12. Why OOP? Composability -- Structured Design Interfaces allow to freely combine modules to produce new systems.
  • 13. Why OOP? Hierarchy Incremental development from small and simple to more complex modules.
  • 14. Why OOP? Continuity Changes and maintenance in only a few modules does not affect the architecture.
  • 15. Main OOP Language Features   Classes (Modularization, structure)   Inheritance / extends (Hierarchy of modules, incremental development   Encapsulation ( Public , Private, Protected)   Composability ( Interfaces / Abstraction )   Polymorphism ( Hierarchy of modules, incremental development)
  • 16. Object in real world
  • 17. Object   Object has own properties and actions   Object can communicate with other object
  • 18. OOP Features in PHP   Class Constants   Autoloading   Constructor and Destructors   Visibility   Inheritance   Static keyword   Class Abstraction   Object Interfaces   Overloading   Object Iteration   Magic Methods  Object Cloning   Type Hinting   Object Serialization
  • 19. Declaration of Class PHP 4 : class Person { var $name = ‘default name’; function Person() { //constructor } } PHP 5 : class Person { public $name = ‘default name’; //automatically calling __construct() magic function }
  • 20. Declaration of Class PHP 4 : class Person {   Should not a PHP reserved var $name = ‘default name’; word function Person()   Starts with a letter or underscore, followed by { any number of letters, //constructor numbers, or underscores } } PHP 5 : class Person { public $name = ‘default name’; //automatically calling __construct() magic function }
  • 21. Object in PHP <?php class Person { public $name; function setName ($personName) { $this->name = $personName; } function getName ( ) { return $this->name; } } //Creating A Object $emran = new Person(); $emran->setName(‘Emran Hasan’); echo $emran->getName(); //Output : Emran Hasan
  • 22. Class   Class is a description of an object   A user-defined data type that contains the variables, properties and methods in it.   Class never executes   It’s possible to create unlimited no of instance from a class. Object   Object is an instance of a class.   It’s an executable copy of a class
  • 23. Using __construct magic function class Person { public $name ; function __construct ($personName) { $this->name = $personName; } function getName ( ) { return $this->name; } } $emran = new Person(‘Emran Hasan’); $alamgir = new Person(‘Alamgir Hossain’); echo $emran->getName(); // will print Emran Hasan echo $alamgir ->getName(); // will print Alamgir Hossain
  • 24. Object Assignment in PHP $emran = new Person(‘Emran Hasan’); $myfriend = $emran; $emran->setName( ‘Md Emran Hasan’); echo $myfriend ->getName(); //Output : Md Emran Hasan
  • 25. Inheritance   It’s an object-oriented concept that helps objects to work.   It defines relationships among classes   Inheritance means that the language gives the ability to extend or enhance existing objects.
  • 26. Simple Class Inheritance class Student extends Person { } $newStudent = new Student (‘Hasin Hyder’); echo $newStudent->getName(); //Output will be : Hasin Hyder
  • 27. Simple Class Inheritance class Student extends Person { protected $roll; function setRoll( $rollNo) { $this->roll = $rollNo; } function getRoll( ) { return $this->roll; } } $newStudent = new Student (‘Hasin Hyder’); $newStudent ->setRoll( ‘98023’); echo $newStudent->getName(); echo “Roll no : ” . $newStudent->getRoll () ; //Output will be : Hasin Hyder , Roll no : 98023
  • 28. Protecting Data With Visibility   Public (default) - the variable can be accessed and changed globally by anything.   Protected - the variable can be accessed and changed only by direct descendants (those who inherit it using the extends statement) of the class and class its self   Private - the variable can be accessed and changed only from within the class
  • 29. Visibility class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // Public echo $obj->protected; // Fatal Error echo $obj->private; // Fatal Error $obj->printHello(); // Shows Public, Protected and Private
  • 30. Visibility class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // Works echo $obj2->private; // Undefined echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined
  • 31. Abstract <?php abstract class Weapon { private $serialNumber; abstract public function fire(); public function __construct($serialNumber) { $this->serialNumber = $serialNumber; } public function getSerialNumber() { return $this->serialNumber; } } ?>
  • 32. Abstraction <?php class Gun extends Weapon $newGun = new Gun(‘AK-47’); { $newGun->fire(); public function fire() { $newGun->getSerialNumber(); echo “gun fired”; } $canon = new Canon(‘CAN-5466’); } $canon->fire(); $canon->getSerialNumber(); class Cannon extends Weapon { public function fire() { echo “canon fired”; } } ?>
  • 33. Interface interface Crud { public function get() {} public function insert() {} public function update() { } public function delete() { } }
  • 34. Interface Class Product implements Crud Class Customer implements Crud { public function get() { public function get() { //here goes the code //here goes the code } } public function insert() { public function insert() { //here goes the code //here goes the code } } public function update() { public function update() { //here goes the code //here goes the code } } public function delete() { public function delete() { //here goes the code //here goes the code } } } }
  • 35. More on OOP   Polymorphism   Coupling   Design Patterns
  • 37. Reference   Everything about PHP http://php.net   Object Oriented Programming http://www.oop.esmartkid.com/index.htm   These slides http://www.apueee.com