Week 22 : Introduction to ObjectOriented Programming using PHP PART A - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Week 22 : Introduction to ObjectOriented Programming using PHP PART A

Description:

The inheriting class can override the definition of existing methods by ... The code of the inheriting class consists only of the changes and additions to the ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 32
Provided by: sand234
Category:

less

Transcript and Presenter's Notes

Title: Week 22 : Introduction to ObjectOriented Programming using PHP PART A


1
Week 22 Introduction to Object-Oriented
Programmingusing PHPPART (A)
2
What is O-O?
  • Design, programming, and languages
  • An alternative to procedural programming
  • anything done using O-O can also be done using a
    procedural paradigm
  • Considering a problem from the perspective of
    objects and how they interact

3
Skills for O-O Programming
  • Must have a solid understanding of subprograms
    and parameters
  • stepwise refinement
  • Should understand references/pointers
  • Coding still involves basic concepts
  • selection structures, repetition structures,

4
O-O vs. Procedural
  • Procedural Paradigm
  • Program defines data and then calls subprogram to
    act on the data
  • Object Paradigm
  • Program creates objects that encapsulate the data
    and procedures that operate on the data

5
Basic Concepts
  • Objects
  • Hidden data that is accessed and manipulated
    through a well-defined interface
  • Classes
  • A template or blueprint for creating objects,
    each with their own data
  • Inheritance
  • A new class created by modification of an
    existing class

6
Objects
  • Part of a program which
  • models some real or conceptual object
  • has behavioural responsibilities (behaviours)
  • has informational responsibilities (attributes)
  • Behaviours (methods)
  • things an object can do
  • like procedures and functions in other languages
  • Attributes (fields)
  • information an object knows (has-a)
  • like data and variables in other languages
    (records)

7
Classes
  • A class is a template. No data is allocated until
    an object is created from the class. However
    attributes and behaviours are generically defined
  • The creation (construction) of an object is
    called instantiation. The created object is often
    called an instance (or an instance of class X)

8
Analogies for Classes Objects
  • Class
  • blueprint
  • pattern
  • cookie cutter
  • factory
  • Object
  • building
  • garment
  • cookie
  • widget

Platos allegory of the cave.
9
Class Diagram
Internal/private/helper methods only available
within the class.
Defined by the class. Filled by the object.
10
Encapsulation - Real Life
  • Bank machine
  • Hidden data
  • account balance
  • personal information
  • Interface
  • deposit, withdraw, transfer
  • display account information

11
Classes - Programming
  • Karel the Robot
  • Hidden data
  • location
  • number of Things
  • Interface
  • move, turnLeft
  • pickThing, putThing

12
Multiple Instances of a Class
  • No limit to the number of objects that can be
    created from a class
  • Each object is independent. Changing one object
    doesnt change the others

13
Interaction Among Classes
  • A program is composed of multiple classes
  • Classes may contain references to other classes
    within the set of attributes or behaviours
  • Start in an application class (main)
  • construct one or more objects and call methods
    associated with those objects

14
Using UML in Design
  • every class has a box
  • class name
  • attributes/instance variables
  • behaviours/methods
  • Arrows indicate the relationships among classes
  • indicates 0-many

Bank

1
15
Inheritance
  • The inheriting class contains all the attributes
    and behaviours of the class it inherited from
    plus any attributes and behaviours it defines
  • The inheriting class can override the definition
    of existing methods by providing its own
    implementation
  • The code of the inheriting class consists only of
    the changes and additions to the base class

16
Inheritance Diagram
General Outline
17
Why Use Inheritance?
  • Modular coding
  • less code, easier to understand
  • Code reuse
  • dont break what is already working
  • easier updates
  • May not have access to modify the original source
    code
  • Polymorphism

18
Inheritance Terminology
  • Class one above
  • Parent class, Super class
  • Class one below
  • Child class
  • Class one or more above
  • Ancestor class, Base class
  • Class one or more below
  • Descendent class

19
Inheritance Bank Account
20
Object-Oriented PHPPART (B)
21
Structure of a class
class Classname var attribute1 function
operation1() function
Classname(param) echo "Constructor"

22
Class structure in PHP 5
class Classname var attribute1 function
__construct(param) echo
"Constructor" function __destruct()

Constructor always named __construct
Will look for function with class name if no
__construct
Can define destructor (no parameters)
23
Instantiating classes
class Classname function Classname(p)
echo "Constructor p\n" a new
Classname('First') b new Classname('Second')
Output
Constructor First Constructor Second
24
Usingclassattributesandoperations
class Classname var foo function
bar(p) this-gtfoo p echo
this-gtfoo."\n" b new
Classname() b-gtbar('wow') echo b-gtfoo."\n"
Output
wow wow
25
Implementing inheritance
class B extends A var attr2 function
op2() b new B() b-gtop1() //
defined in A
Attributes and operations defined in parent are
available in child class.
26
Overriding
  • Can override operations in parent
  • No way to access original in PHP 4
  • PHP 5 provides access with parent keyword
  • parentop1()
  • PHP 5 adds final keyword
  • Operations declared as final cannot be overridden

final function op1()
27
Multiple inheritance(actually the lack thereof)
  • PHP does not allow multiple inheritance
  • Can define interfaces in PHP 5
  • Similar to interfaces in Java

28
New OOP features in PHP 5
  • __get and __set
  • public, protected, private
  • Per-class constants
  • Static methods
  • instanceof and type hinting
  • Cloning and __clone
  • Abstract classes
  • __toString and other special functions

29
__get and __set
class Classname var att1 function
__get(name) return this-gtname
function __set(name, value)
this-gtname value
Calls __set
What will the values of the parameters be?
30
instanceof and type hinting
True if b is an object of class B
function check_hint(B obj) //
check_hint(a)
Error if a is not an object of class B
31
PHP 5 vs. PHP 4Two notes
  • Objects are passed by reference in PHP 5
  • Passed by value in PHP 4
  • Pass by reference is more efficient
  • PHP 4 has "difficulty dereferencing objects that
    were returned from functions"
  • e.g. select_object()-gtdisplay()
  • This problem is fixed in PHP 5.
Write a Comment
User Comments (0)
About PowerShow.com