MCIS/UA PHP Training 2003 Chapter 5 Object Oriented Concepts - - PowerPoint PPT Presentation

mcis ua
SMART_READER_LITE
LIVE PREVIEW

MCIS/UA PHP Training 2003 Chapter 5 Object Oriented Concepts - - PowerPoint PPT Presentation

MCIS/UA PHP Training 2003 Chapter 5 Object Oriented Concepts Datasource/Database Classes OOP myth OOP has NOTHING to do with graphics or graphical user interfaces (GUIs). scenario Why OOP? Imagine the following scenario... The


slide-1
SLIDE 1

PHP Training 2003 Chapter 5 Object Oriented Concepts Datasource/Database Classes

MCIS/UA

slide-2
SLIDE 2

OOP myth

  • OOP has NOTHING to do with graphics
  • r graphical user interfaces (GUIs).

scenario

slide-3
SLIDE 3

Why OOP?

  • Imagine the following scenario...
  • The year is 2007
  • Miami is currently running Banner version 9.1.5
  • PHP has taken root and we now have 12,000

PHP files and applications.

  • You read your email and find that Banner version

10.0 has been released

  • One of the "features" is that SPBPERS is being

split into 5 separate tables

slide-4
SLIDE 4

Why OOP?

  • Scenario #1
  • 7,000 of the 12,000 PHP files have to be

modified to account for this change.

  • Scenario #2
  • 5 of the 12,000 PHP files have to be modified to

account for this change.

real life scenario

slide-5
SLIDE 5

Why OOP?

  • Real life scenario...
  • The year is 2003
  • We've deployed 83+ Cold Fusion applications

that authenticate via LDAP

  • The LDAP authentication code was copied from

program to program

  • Next month, Technical Services is changing the

way LDAP works.

slide-6
SLIDE 6

Why OOP?

  • Scenario #1
  • Fix 83+ Cold Fusion programs in the next month
  • Scenario #2
  • Fix 1 library that all programs are using to do

authentication

  • Scenario #3
  • Fix 1 library that all programs are using to do

authentication AND add single-signon capabilities to all of them

intro

slide-7
SLIDE 7

Why OOP?

  • Think about an application that you wrote

recently...

  • Did you write any code that exists in another

application?

  • Did you reuse that code (not a copy...but the

actual code)?

  • How much work would it take to make a major

change to that program?

  • For example, switching from using an array or

flat file to using a database?

  • Or splitting a single database table into 5?

intro

slide-8
SLIDE 8

OOP Introduction

  • OOP is an evolutionary step in the history
  • f programming concepts

Spaghetti Code ↓ Modular Programming ↓ Structured Programming ↓ Object Oriented Programming

slide-9
SLIDE 9

OOP Introduction

  • OOP is designed to facilitate the following:
  • Code Reuse - goal is 0% code duplication
  • Easier Development (especially for large

projects)

  • Distributed Development
  • Increased Quality
  • Better model the real world
  • Easier Maintenance and Enhancement

slide-10
SLIDE 10

OOP Introduction

  • OOP requires a rethinking of code design
  • Tying the data and the operations

performed on that data together into an

  • bject
  • Typically, data is hidden from all but the
  • bject itself - blackbox approach
  • You may already be using many of the

concepts and you don't know it

int example

slide-11
SLIDE 11

Conceptual Example

  • Integer
  • Value

5, -27, 0, etc.

  • Actions:

Creation Assignment Addition, Subtraction, Multiplication, etc. Retrieval

slide-12
SLIDE 12

Conceptual Example

  • How are integers stored internally?
  • in memory?
  • in registers?
  • swapped out on disk?
  • What would happen to your code if the

next version of the compiler changed the way integers were stored?

  • 32-bit to 64-bit?

slide-13
SLIDE 13

Conceptual Example

terminology

Creation int $i;

(not required in PHP)

Assignment $i = 5; Operations $i + 3; Retrieval print $i; person $p; $p's name is "Mary Smith" Generate bill for $p print $p;

slide-14
SLIDE 14

OOP term Description

int $i; $i = 5; $i + 5; Person $p; $p's name is Mary generate $p's bill

Class The "data type" int Person Object / Instance An instance of the data type $i $p Property/ attribute data associated with the object integer value (5) name (Mary) Method functions associated with the object + generate bill

OOP Terminology

slide-15
SLIDE 15

OOP Terminology

  • Encapsulation
  • Hiding the internal data structures from

the rest of the program

  • Allows the internal data structures to

change without recoding the program.

  • Blackbox approach
  • For Person, instead of accessing the name

directly, use getName() and setName().

slide-16
SLIDE 16

OOP Terminology

  • Inheritance
  • Creating a new class that is an extension
  • f another class

db class

undergraduate student → graduate person → classified employee → unclassified

slide-17
SLIDE 17

Datasource/Database Classes

  • As an example of using OOP

, I will be demonstrating the Datasources and Database classes.

  • Used to access databases (and other

resources)

  • Designed to be relatively database independent
  • Oracle
  • mySQL

slide-18
SLIDE 18

Datasource/Database Classes

  • Written in-house by Net Apps
  • Documented at:

http://webdev.admin.muohio.edu/phpapps/envdocs/ info table

slide-19
SLIDE 19

Info Table

datasources

UniqueId Name Address Phone covertka Kent Covert 352 Gaskill 529-7317 kingmatm Tim Kingman 354 Gaskill 529-5330 moosejc John Moose 354 Gaskill 529-1427 tepeds Dirk Tepe 357 Gaskill 529-1514

slide-20
SLIDE 20

Datasources

  • Similar to datasources in Cold Fusion, but

expanded

  • Datasources are used to access certain resources

(e.g. databases, LDAP , etc.)

  • Datasources allow developers to access resources

without hardcoding usernames, passwords, database instances into their applications.

  • Datasources allow data admin to change resource

characteristics without modifying applications.

slide-21
SLIDE 21

Datasources

  • Datasources are/will be created by Data

Admin

  • Datasources are referenced by name

slide-22
SLIDE 22

Datasources

  • Database datasources:
  • contain
  • database type (Oracle, mySQL, etc.)
  • host
  • port
  • database instance (PROD, MUCC, etc.)
  • username and password

creating instances

slide-23
SLIDE 23

Creating a new instance

  • New instances of a class can be

created using the new keyword. $instance = new className(...);

  • Creates a new instance of the class

"className"

  • The new instance is returned as a reference

(and is stored in $instance in this example)

  • This reference ($instance) is used for all

future interaction with this instance

slide-24
SLIDE 24

Creating a new instance

$info = new DataSource("info");

$prod = new DataSource("Prod");

$time = new DataSource("TimeEntry");

methods

slide-25
SLIDE 25

Calling Methods

  • Methods and attributes are accessed by using

the -> operator.

$instance->methodName(parameters, ...)

  • This will call the methodName method of the

$instance instance.

$info = new DataSource("info"); $time = new DataSource("timeEntry"); perform

slide-26
SLIDE 26

perform database method

  • The perform() method can be used to issue

simple SQL (non-select) queries.

$info = new DataSource("info"); $info->perform("DELETE FROM info " . "WHERE uniqueId = 'covertka'");

  • Returns true on success, false otherwise

bad

slide-27
SLIDE 27

perform database method

$uniqueId = $_REQUEST["uniqueId"]; $info->perform("DELETE FROM info " . "WHERE value = '$uniqueId'"); print $uniqueId; ' OR '' = ' $info->perform("DELETE FROM info " . "WHERE uniqueId = '' OR '' = ''"); placeholders

slide-28
SLIDE 28

perform database method

$info = new DataSource("info"); $uniqueId = 'covertka'; $info->perform("DELETE FROM info " . "WHERE uniqueId = ?", $uniqueId); $info->perform("DELETE FROM info " . "WHERE uniqueId = '\' OR \'\' = \''"); commit

slide-29
SLIDE 29

commit database method

  • The commit() method can be used to

commit database changes

$info = new DataSource("info"); $info->perform("DELETE FROM info " . "WHERE uniqueId = ?", $uniqueId); $info->commit();

  • Available for all databases but doesn't

necessarily do anything (database dependent)

rollback

slide-30
SLIDE 30

rollback database method

  • The rollback() method can be used to undo

database changes

$info = new DataSource("info"); $info->perform("DELETE FROM info " . "WHERE uniqueId = ?", $uniqueId"); $info->rollback();

  • Available for all databases but doesn't

necessarily do anything (database dependent)

rollback

slide-31
SLIDE 31

Handling Selects

  • Many methods for handling selects
  • many of the differences are purely for

convenience

queryFirstColumn() queryFirstRow_array() queryFirstRow_assoc() queryAll_array() queryAll_assoc() prepare()

queryFirstColumn

slide-32
SLIDE 32

queryFirstColumn()

  • Used to access a single value from a select
  • Returns the first column of the first row

$value = $dbh->queryFirstColumn($statement, ...); $info = new DataSource("info"); $name = $info->queryFirstColumn( "SELECT name ". "FROM info ". "WHERE uniqueid = 'covertka'"); print $name; → Kent Covert

slide-33
SLIDE 33

queryFirstColumn()

$info = new DataSource("info"); $name = $info->queryFirstColumn( "SELECT name ". "FROM info ". "WHERE uniqueid = ?", $uid); print $name; → Kent Covert

queryFirstRow_array

slide-34
SLIDE 34

queryFirstRow_array()

  • Used to access a single row from a select
  • Returns the first row as a numerically indexed

array

$array = $dbh->queryFirstRow_array($statement, ...);

slide-35
SLIDE 35

queryFirstRow_array()

$info = new DataSource("info"); $row = $info->queryFirstRow_array( "SELECT name, address, phone ". "FROM info ". "WHERE uniqueid = ?", $uid); print $row[0]; → Kent Covert print $row[1]; → 340 Gaskill Hall print $row[2]; → 529-7317

queryFirstRow_assoc

slide-36
SLIDE 36

queryFirstRow_assoc()

  • Similar to queryFirstRow_array()
  • Returns the first row as an associative array

$array = $dbh->queryFirstRow_assoc($statement, ...);

slide-37
SLIDE 37

queryFirstRow_assoc()

$info = new DataSource("info"); $row = $info->queryFirstRow_assoc( "SELECT name, address, phone ". "FROM info ". "WHERE uniqueid = ?", $uid);

print $row['name']; → Kent Covert print $row['address']; → 340 Gaskill Hall print $row['phone']; → 529-7317

queryAll_array

slide-38
SLIDE 38

queryAll_array()

  • Returns all the rows of a select
  • Returned as a numerically indexed array of

associative arrays

$array = $dbh->queryAll_array($statement, ...);

slide-39
SLIDE 39

queryAll_array()

$info = new DataSource("info"); $rows = $info->queryAll_array( "SELECT uniqueId, name, address, phone ". "FROM info"); print $rows[0]['name']; → Kent Covert print $rows[3]['name']; → Dirk Tepe print $rows[3]['uniqueId']; → tepeds

queryAll_assoc

slide-40
SLIDE 40

queryAll_assoc()

  • Returns all the rows of a select
  • Returned as an associative array of associative

arrays keyed on the first column of the result

$array = $dbh->queryAll_assoc($statement, ...);

slide-41
SLIDE 41

queryAll_assoc()

$info = new DataSource("info"); $rows = $info->queryAll_assoc( "SELECT uniqueId, name, address, phone ". "FROM info");

print $rows['covertka']['name']; → Kent Covert print $rows['tepeds']['name']; → Dirk Tepe print $rows['tepeds']['uniqueId']; →

prepare

slide-42
SLIDE 42

prepare()

  • Allows custom handling of result sets
  • Handles repeated queries easily

$sth = $dbh->prepare($statement);

  • Notice no placeholders - handled when

executed

  • Returns a statement handle object, or false on

error

prepare example

slide-43
SLIDE 43

prepare()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?");

statement handle

slide-44
SLIDE 44

Statement Handle Class

  • Used to repeatedly execute the same

statement with different values

  • Loop over the rows returned by a select

execute() fetchRow_array() fetchRow_assoc() seek_forward() free() current_row attribute

execute

slide-45
SLIDE 45

Statement Handle execute()

  • Executes a previously prepared statement

$sth->execute(placeholders);

  • Returns true on success, false otherwise

execute example

slide-46
SLIDE 46

Statement Handle execute()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); . . . $address = "352 Gaskill Hall"; $sth->execute($address);

fetchRow_array()

slide-47
SLIDE 47

Statement Handle fetchRow_array()

  • Fetches the next row in the result set as a

numerically indexed array

  • Repeated calls fetch the next row until none

are left

$array = $sth->fetchRow_array();

  • Returns false when no rows left

fetchRow_array example

slide-48
SLIDE 48

Statement Handle fetchRow_array()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); while($row = $sth->fetchRow_array()) { print $row[1]; } Tim Kingman John Moose

another execute

slide-49
SLIDE 49

Statement Handle fetchRow_array()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); while($row = $sth->fetchRow_array()) { print $row[1]; } $address = "352 Gaskill Hall"; $sth->execute($address); while($row = $sth->fetchRow_array()) { print $row[1]; }

fetchRow_assoc

slide-50
SLIDE 50

Statement Handle fetchRow_assoc()

  • Similar to fetchRow_array()
  • Returns associative array rather than a

numerically indexed array

$array = $sth->fetchRow_assoc();

  • Returns false when no rows left

fetchRow_assoc example

slide-51
SLIDE 51

Statement Handle fetchRow_assoc()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); while($row = $sth->fetchRow_assoc()) { print $row['name']; } Tim Kingman John Moose

current_row

slide-52
SLIDE 52

Statement Handle current_row attribute

  • The current_row attribute contains the

number of the row about to be fetched

  • Starts at 1

$row = $sth->current_row;

  • Notice that there are no (). This is an attribute
  • not a function.
  • Contains false when no rows left

current_row example

slide-53
SLIDE 53

Statement Handle current_row attribute

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "352 Gaskill Hall"; $sth->execute($address); print $sth->current_row; → 1 print $sth->fetchRow_assoc(); → Array print $sth->current_row; → 2 print $sth->fetchRow_assoc(); → false print $sth->current_row; → false

seek_forward

slide-54
SLIDE 54

Statement Handle seek_forward()

  • The seek_forward() method can be used to

move forward in the result set

  • It cannot move backwards through the result

set

$sth->seek_forward($row);

seek_forward example

slide-55
SLIDE 55

Statement Handle seek_forward()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); $sth->seek_forward(2); $row = $sth->fetchRow_assoc(); print $row['name']; John Moose

db cleanup

slide-56
SLIDE 56

DB Cleanup

  • All Statement Handles should be disposed of

using the free() method.

$sth->free();

  • All database handles should be disposed of

using the disconnect() method.

$dbh->disconnect();

db cleanup example

slide-57
SLIDE 57

Statement Handle seek_forward()

$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?"); $address = "354 Gaskill Hall"; $sth->execute($address); $row = $sth->fetchRow_assoc(); print $row['name']; $sth->free(); $info->disconnect();

bind_by_name method

slide-58
SLIDE 58

Bind_by_name method

  • The Statement Handle method

bind_by_name() is used to bind PHP variables to named variables in SQL statements.

  • Used with stored procedures
  • Oracle specific

$sth->bind_by_name($placeholder_name, $variable[, $length]);

  • See example in the documentation

questions

slide-59
SLIDE 59

Questions?

slide-60
SLIDE 60

Homework #5