mcis ua
play

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

MCIS/UA PHP Training 2003 Chapter 8 Object-Oriented Concepts - Part 2 Introduction OOP was introduced in Chapter 5. Learned how to use classes that were already defined. Chapter 8 will focus on creating your own classes. As an


  1. MCIS/UA PHP Training 2003 Chapter 8 Object-Oriented Concepts - Part 2

  2. Introduction • OOP was introduced in Chapter 5. • Learned how to use classes that were already defined. • Chapter 8 will focus on creating your own classes. • As an example, we will develop an organization class. • The example is for demonstration purposes and may not be the best way to implement this class. • This is pure PHP . ->

  3. Introduction • PHP 4 has functional, but limited OOP support • PHP 5 is expected to expand OOP support hands-on agenda

  4. Hands-on Agenda • Create an organization class/object to manage all the operations of an organization • Functions • get a list of all the organizations • get information for an organization • modify information for an organization defining

  5. Defining a class • Classes are defined using the class keyword: class classname { } • Classnames follow the same rules as function names. do

  6. Defining a class organization.php <?php class organization { } ?> ->

  7. Defining a class Application.php <?php $cfg_appname = "My Application"; $cfg_sessionname = "covertka_myapp"; require_once "organization.php"; $presentation = new MU_Presentation(); [...] methods

  8. Methods • As discussed previously, functions in a class are called methods. • There are 2 types of methods: • static or class methods • called on the class itself • not tied to a particular instance • instance methods • called on an instance of the class do

  9. Static Methods • Static methods are called on the class itself. • Static methods are declared within the class just like any other function. • Static methods are called using the :: operator rather than the -> operator classname :: methodname ( params ...) $orgs = organization::allOrgs(); do

  10. Static Methods organization.php <?php class organization { function allOrgs() { $dbh = new dataSource("PHP_Training_Banner"); $query = "select shortname from orgs"; return $dbh->queryall_list($query); } } ?> methods

  11. Static Methods view.php <?php [...] $dbh = new dataSource("PHP_Training_Banner"); $query = "select * from orgs $order_by"; $data = $dbh->queryAll_array($query); $recordCount = count($data); $orgs = organization::allOrgs(); $recordCount = count($orgs); [...] ?> instance methods

  12. Instance Methods • Instance methods are called on an instance of a class. • Instance methods are declared within the class just like any other function. • Instance methods are called using the -> operator $org = new organization("chess"); $desc = $org->get("description"); ->

  13. Instance Methods • The pre-defined variable $this can be used in your class to refer this particular instance. $org1 = new organization("chess"); $org2 = new organization("musf"); $desc1 = $org1->get("description"); constructors

  14. Constructor Methods • Constructor methods are called when an instance is created using the new keyword. • Constructor methods have the same name as the name of you class. class organization { function organization() { [...] } } do

  15. Constructor Methods organization.php <?php class organization { var $dbh; var $data; function organization($sn = "") { $this->dbh = new dataSource("PHP_Training_Banner"); if ($sn) { $query = "select shortname, description,". "to_char(startdate,'mm/dd/yyyy') as startdate,". "to_char(enddate,'mm/dd/yyyy') as enddate, advisor ". "from orgs where shortname=?"; $this->data = $this->dbh->queryFirstRow_assoc($query, $sn); } } function allOrgs() { $dbh = new dataSource("PHP_Training_Banner"); -> $query = "select shortname from orgs";

  16. Constructor Methods view.php <?php [...] for($i = $start - 1; $i < $start + $show - 1 && $i < $recordCount; $i++) { $row = new Organization($orgs[$i]); $tabulator_row =& $table->add_element('tabulator_row'); $tabulator_row->add_element('tabulator_row_href', NULL, 'modify.php?shortname='.urlencode($orgs[$i])); [...] ?> ->

  17. Instance Methods organization.php <?php class organization { [...] function get($attribute) { return $this->data[$attribute]; } function getAllData() { return $this->data; } } ?> ->

  18. Instance Methods view.php <?php [...] for($i = $start - 1; $i < $start + $show - 1 && $i < $recordCount; $i++) { $org = new Organization($orgs[$i]); $tabulator_row =& $table->add_element('tabulator_row'); $tabulator_row->add_element('tabulator_row_href', NULL, 'modify.php?shortname='.urlencode($orgs[$i])); foreach($org->getAllData() as $column => $value) { $tabulator_row->add_element('tabulator_row_data', array('name' => $column), htmlspecialchars($value)); } } [...] ?> instance variables

  19. Instance Variables • Variables that hold information specific to this instance are called instance variables. • Instance variables should be declared at the beginning of the class using the var keyword. var variablename ; var variablename = value ; var $dbh; • Instance variables are accessed similarly to instance methods $ instance -> variablename ; $dbh->statement_tracking = true; do

  20. Instance Variables organization.php <?php class organization { var $dbh; var $data; var $isValid = false; function organization($sn = "") { $this->dbh = new dataSource("PHP_Training_Banner"); if ($sn) { $query = "select shortname, description,". "to_char(startdate,'mm/dd/yyyy') as startdate,". "to_char(enddate,'mm/dd/yyyy') as enddate, advisor ". "from orgs where shortname=?"; $this->data = $this->dbh->queryFirstRow_assoc($query, $sn); if ($this->data) { $this->isValid = true; } } } inheritance function allOrgs() {

  21. Inheritance • Classes in PHP can extend other classes. • In doing so, they inherit all of the properties and methods of the original class. • The extend keyword is used to indicate the class you are extending. class facultyOrganization extends organization { var $department = ""; . . . } serialization

  22. Serialization • Serialization allows an application to convert an instance into a string representation of the instance. • The serialize() and unserialize() functions are used for this purpose. $org = new Organization($orgs[$i]); � $sorg = serialize($org); print $sorg; output

  23. Serialization • O:12:"organization":2:{s:3:"dbh";O:8:"dbh_oci8":16:{s:4:"u ser";s:11:"PHPTRAINING";s:8:"database";s:4:"UAP5";s:4:"typ e";s:4:"oci8";s:9:"connected";b:1;s:2:"db";i:0;s:7:"driver s";N;s:5:"query";s:202:" select shortname, description, to_char(startdate,'mm/dd/yyyy') as startdate, to_char(enddate,'mm/dd/yyyy') as enddate, advisor from orgs where shortname='chess'";s:11:"auto_commit";b:1;s:18:"statement_ tracking";b:0;s:11:"_statements";a:0:{}s:5:"error";s:0:""; s:12:"error_string";N;s:9:"error_num";N;s:13:"trigger_erro r";b:1;s:19:"default_date_format";s:22:"DD-MON-YYYY HH24:MM:SS";s:6:"result";i:0;}s:4:"data";a:5:{s:9:"shortna me";s:5:"chess";s:11:"description";s:41:"Miami University Chess Club - Check Mates";s:9:"startdate";s:10:"06/27/ 2010";s:7:"enddate";s:10:"07/01/ 1988";s:7:"advisor";s:8:"covertka";}} unserialize

  24. Serialization • Unserializing is performed using the unserialize() function. � $org = unserialize($sorg); __sleep & __wakeup

  25. Serialization • The __sleep() method of a class, if present, is called at the beginning of the serialization process in order for the class to perform any cleanup tasks. • The __wakeup() method of a class, if present, is called at the end of the unserialization process in order for the class to perform any initialization tasks. end

  26. Questions?

Recommend


More recommend