Title: Week 22 : Introduction to ObjectOriented Programming using PHP PART A
1Week 22 Introduction to Object-Oriented
Programmingusing PHPPART (A)
2What 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
3Skills 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,
4O-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
5Basic 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
6Objects
- 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)
7Classes
- 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)
8Analogies for Classes Objects
- Class
- blueprint
- pattern
- cookie cutter
- factory
- Object
- building
- garment
- cookie
- widget
Platos allegory of the cave.
9Class Diagram
Internal/private/helper methods only available
within the class.
Defined by the class. Filled by the object.
10Encapsulation - Real Life
- Bank machine
- Hidden data
- account balance
- personal information
- Interface
- deposit, withdraw, transfer
- display account information
11Classes - Programming
- Karel the Robot
- Hidden data
- location
- number of Things
- Interface
- move, turnLeft
- pickThing, putThing
12Multiple 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
13Interaction 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
14Using 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
15Inheritance
- 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
16Inheritance Diagram
General Outline
17Why 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
18Inheritance 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
19Inheritance Bank Account
20Object-Oriented PHPPART (B)
21Structure of a class
class Classname var attribute1 function
operation1() function
Classname(param) echo "Constructor"
22Class 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)
23Instantiating classes
class Classname function Classname(p)
echo "Constructor p\n" a new
Classname('First') b new Classname('Second')
Output
Constructor First Constructor Second
24Usingclassattributesandoperations
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
25Implementing 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.
26Overriding
- 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()
27Multiple inheritance(actually the lack thereof)
- PHP does not allow multiple inheritance
- Can define interfaces in PHP 5
- Similar to interfaces in Java
28New 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?
30instanceof 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
31PHP 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.