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 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 of 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 object • Typically, data is hidden from all but the object 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 int $i; Creation person $p; (not required in PHP) $p's name is Assignment $i = 5; "Mary Smith" Generate bill Operations $i + 3; for $p Retrieval print $i; print $p; terminology
OOP Terminology int $i; Person $p; OOP Description $i = 5; $p's name is Mary term $i + 5; generate $p's bill Class The "data type" int Person Object / An instance of the $i $p Instance data type Property/ data associated with integer value (5) name (Mary) attribute the object Method functions associated + generate bill with the object →
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 of another class undergraduate → student graduate person → classified employee → unclassified db class
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 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
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, ... ); →
Recommend
More recommend