mcis ua
play

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


  1. MCIS/UA PHP Training 2003 Chapter 5 Object Oriented Concepts Datasource/Database Classes

  2. OOP myth • OOP has NOTHING to do with graphics or graphical user interfaces (GUIs). scenario

  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 →

  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

  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. →

  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

  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

  8. OOP Introduction • OOP is an evolutionary step in the history of programming concepts Spaghetti Code ↓ Modular Programming ↓ Structured Programming ↓ Object Oriented Programming →

  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 →

  10. 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

  11. Conceptual Example • Integer • Value 5, -27, 0, etc. • Actions: Creation Assignment Addition, Subtraction, Multiplication, etc. Retrieval →

  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? →

  13. 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

  14. 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 →

  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(). →

  16. OOP Terminology • Inheritance • Creating a new class that is an extension of another class undergraduate → student graduate person → classified employee → unclassified db class

  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 →

  18. Datasource/Database Classes • Written in-house by Net Apps • Documented at: http://webdev.admin.muohio.edu/phpapps/envdocs/ info table

  19. 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

  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. →

  21. Datasources • Datasources are/will be created by Data Admin • Datasources are referenced by name →

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

  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 →

  24. Creating a new instance $info = new DataSource("info"); $prod = new DataSource("Prod"); $time = new DataSource("TimeEntry"); methods

  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

  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

  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

  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

  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

  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

  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

  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 →

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

  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, ... ); →

  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

  36. queryFirstRow_assoc() • Similar to queryFirstRow_array() • Returns the first row as an associative array $array = $dbh ->queryFirstRow_assoc( $statement, ... ); →

Recommend


More recommend