Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon Page 1 23 – Object Oriented Programming in PhP.

Similar presentations


Presentation on theme: "Mark Dixon Page 1 23 – Object Oriented Programming in PhP."— Presentation transcript:

1 Mark Dixon Page 1 23 – Object Oriented Programming in PhP

2 Mark Dixon Page 2 Questions: HTML in PhP Are these correct (assume variables and fields exist)? $s = $s.. $r["Model"]; $s = $s $r["Length"]; $h = " ". $h. " ";  

3 Mark Dixon Page 3 Questions: SQL in VB Are these correct (assume variables and fields exist)? $id = 4; $sql = SELECT * FROM Customer; $sql = $sql " WHERE CustID = ". $id;  

4 Mark Dixon Page 4 Questions: Writing to Databases What SQL statement adds a new record to a database table? What SQL statement removes a record in a database table? What SQL statement changes data in a record? INSERT DELETE UPDATE

5 Mark Dixon Page 5 Session Aims & Objectives Aims –To introduce object oriented techniques in PhP Objectives, by end of this week’s sessions, you should be able to: –create a class definition in server-side code –create an instance of a class –create a class definition from a class diagram

6 Mark Dixon Page 6 Object-Oriented Paradigm A program is made up of a number of objects that communicate with each other by passing messages Each object contains –attributes/properties that represent its state, and –operations/methods that represent its behaviour Objects often mirror the real world –Customers –Students –Patients

7 Mark Dixon Page 7 Classes and Instances Object Classes –general descriptions of types of objects, e.g. student, product, customer, lecturer, and room. Object Instances –specific items of a given class, e.g. each of you could be an instance of the student class Room 214 could be an instance of the room class I could be an instance of the lecturer class Bolt could be an instance of the part class

8 Mark Dixon Page 8 Object Concepts - Implementation Properties – implemented as –data structures (variables, arrays, and types). Methods – implemented as either –a procedure (to perform some processing), or –a function (to return a value). Object oriented paradigm builds on (rather than replaces) the structured paradigm

9 Mark Dixon Page 9 Class Diagrams Used to describe structure of object classes: Module Code: string Title: string GetTitle(): string SetTitle(t: string) Count(): integer Class Attributes/Properties Class Operations/Methods Class Name

10 Mark Dixon Page 10 class Module{ var $Code; var $Title; function GetTitle(){ return $this->Title; } function SetTitle($t){ $this->Title = $t; } function Count(){ return 50; } Implementing Class Diagrams Module Code: String Title: String GetTitle(): string SetTitle(t: string) Count(): integer

11 Mark Dixon Page 11 Benefits of OOP in code Procedures and Functions are part of object –encapsulation Related Data and Operations together Clearer code Less prone to error

12 Mark Dixon Page 12 Example: Counter (html) Counter <?php echo $Msg; ?>

13 Mark Dixon Page 13 Example: Counter (code) <?php session_start(); if(isset($_SESSION["c"])){ $c = $_SESSION["c"]; if(isset($_POST["btnReset"])){ $c->Reset(); }elseif(isset($_POST["btnUp"])){ $c->Up(); }elseif(isset($_POST["btnDown"])){ $c->Down(); } $Msg = $c->GetCount(); }else{ $_SESSION["c"] = new Counter; $Msg = ""; } ?> <?php class Counter{ var $mCount; function GetCount(){ return $this->mCount; } function Reset(){ $this->mCount = 0; } function Up(){ $this->mCount = $this->mCount + 1; } function Down(){ $this->mCount = $this->mCount - 1; } ?> Counter.php

14 Mark Dixon Page 14 Questions: OOP How many –classes –properties –methods –functions –procedures <?php class Counter{ var $mCount; function GetCount(){ return $this->mCount; } function Reset(){ $this->mCount = 0; } function Up(){ $this->mCount = $this->mCount + 1; } function Down(){ $this->mCount = $this->mCount - 1; } ?> 1 1 4 1 3

15 Mark Dixon Page 15 Tutorial Exercise: Counter Task 1: Get the Counter example from the lecture working. Task 2: Modify your code – so that the value cannot go below 0 or above 10.


Download ppt "Mark Dixon Page 1 23 – Object Oriented Programming in PhP."

Similar presentations


Ads by Google