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
ZIP
Object Oriented PHP5
PPT
Php Oop
PDF
7 rules of simple and maintainable code
PDF
Object Oriented Programming with PHP 5 - More OOP
PDF
A Gentle Introduction To Object Oriented Php
PPT
Php object orientation and classes
PPT
Architecture logicielle #3 : object oriented design
Object Oriented PHP5
Php Oop
7 rules of simple and maintainable code
Object Oriented Programming with PHP 5 - More OOP
A Gentle Introduction To Object Oriented Php
Php object orientation and classes

What's hot (20)

PPTX
Clean Code: Chapter 3 Function
PDF
OOP in PHP
PPT
Class 7 - PHP Object Oriented Programming
PDF
PHPID online Learning #6 Migration from procedural to OOP
PDF
Design patterns in PHP
PPTX
Oop in-php
PDF
Functions in PHP
PDF
Object Oriented Programming in PHP
PDF
Developing SOLID Code
PDF
Dependency Injection Smells
KEY
Clean code and Code Smells
KEY
Module Magic
PPT
Oops in PHP
PDF
Advanced PHP Simplified - Sunshine PHP 2018
PPT
Class and Objects in PHP
PPTX
Object Oriented Programming Basics with PHP
PDF
Intermediate OOP in PHP
PPTX
Introduction to PHP OOP
PPT
PHP- Introduction to Object Oriented PHP
Clean Code: Chapter 3 Function
OOP in PHP
Class 7 - PHP Object Oriented Programming
PHPID online Learning #6 Migration from procedural to OOP
Design patterns in PHP
Oop in-php
Functions in PHP
Object Oriented Programming in PHP
Developing SOLID Code
Dependency Injection Smells
Clean code and Code Smells
Module Magic
Oops in PHP
Advanced PHP Simplified - Sunshine PHP 2018
Class and Objects in PHP
Object Oriented Programming Basics with PHP
Intermediate OOP in PHP
Introduction to PHP OOP
PHP- Introduction to Object Oriented PHP
Ad

Similar to OOP in PHP (20)

PPTX
Oopsinphp
PPTX
Ch8(oop)
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
Php oop (1)
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Object Oriented PHP - PART-1
PPT
Advanced php
PPTX
Only oop
PPTX
Object oriented programming in php
PDF
Demystifying Object-Oriented Programming #ssphp16
PDF
Object Oriented Programming in PHP
PDF
Object_oriented_programming_OOP_with_PHP.pdf
PPT
Introduction to OOP with PHP
PDF
09 Object Oriented Programming in PHP #burningkeyboards
DOCX
Oops concept in php
PDF
OOPs Concept
PDF
Demystifying Object-Oriented Programming #phpbnl18
Oopsinphp
Ch8(oop)
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Php oop (1)
Lecture-10_PHP-OOP.pptx
Object Oriented PHP - PART-1
Advanced php
Only oop
Object oriented programming in php
Demystifying Object-Oriented Programming #ssphp16
Object Oriented Programming in PHP
Object_oriented_programming_OOP_with_PHP.pdf
Introduction to OOP with PHP
09 Object Oriented Programming in PHP #burningkeyboards
Oops concept in php
OOPs Concept
Demystifying Object-Oriented Programming #phpbnl18
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Insiders guide to clinical Medicine.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Types and Its function , kingdom of life
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
01-Introduction-to-Information-Management.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Microbial diseases, their pathogenesis and prophylaxis
Renaissance Architecture: A Journey from Faith to Humanism
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
102 student loan defaulters named and shamed – Is someone you know on the list?

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
  • 36. Questions? Thanks Tarek Mahmud Apu Founder, Wneeds apu.eee@wneeds.com
  • 37. Reference   Everything about PHP http://php.net   Object Oriented Programming http://www.oop.esmartkid.com/index.htm   These slides http://www.apueee.com