PHP Training 2003 Chapter 5 Object Oriented Concepts Datasource/Database Classes
MCIS/UA PHP Training 2003 Chapter 5 Object Oriented Concepts - - PowerPoint PPT Presentation
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
OOP myth
- OOP has NOTHING to do with graphics
- r graphical user interfaces (GUIs).
scenario
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
→
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
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.
→
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
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
OOP Introduction
- OOP is an evolutionary step in the history
- f programming concepts
Spaghetti Code ↓ Modular Programming ↓ Structured Programming ↓ Object Oriented Programming
→
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
→
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
Conceptual Example
- Integer
- Value
5, -27, 0, etc.
- Actions:
Creation Assignment Addition, Subtraction, Multiplication, etc. Retrieval
→
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?
→
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;
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
→
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().
→
OOP Terminology
- Inheritance
- Creating a new class that is an extension
- f another class
db class
undergraduate student → graduate person → classified employee → unclassified
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
→
Datasource/Database Classes
- Written in-house by Net Apps
- Documented at:
http://webdev.admin.muohio.edu/phpapps/envdocs/ info table
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
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.
→
Datasources
- Datasources are/will be created by Data
Admin
- Datasources are referenced by name
→
Datasources
- Database datasources:
- contain
- database type (Oracle, mySQL, etc.)
- host
- port
- database instance (PROD, MUCC, etc.)
- username and password
creating instances
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
→
Creating a new instance
$info = new DataSource("info");
$prod = new DataSource("Prod");
$time = new DataSource("TimeEntry");
methods
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
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
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
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
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
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
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
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
→
queryFirstColumn()
$info = new DataSource("info"); $name = $info->queryFirstColumn( "SELECT name ". "FROM info ". "WHERE uniqueid = ?", $uid); print $name; → Kent Covert
queryFirstRow_array
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, ...);
→
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
queryFirstRow_assoc()
- Similar to queryFirstRow_array()
- Returns the first row as an associative array
$array = $dbh->queryFirstRow_assoc($statement, ...);
→
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
queryAll_array()
- Returns all the rows of a select
- Returned as a numerically indexed array of
associative arrays
$array = $dbh->queryAll_array($statement, ...);
→
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
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, ...);
→
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
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
prepare()
$info = new DataSource("info"); $sth = $info->prepare( "SELECT uniqueId, name, address, phone ". "FROM info ". "WHERE address = ?");
statement handle
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
Statement Handle execute()
- Executes a previously prepared statement
$sth->execute(placeholders);
- Returns true on success, false otherwise
execute example
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()
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
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
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
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
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
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
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
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
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
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
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
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