object oriented programming in php. list of items of interest what is a class what is an object what...

25
Object Oriented Programming in PHP

Upload: carmel-chambers

Post on 03-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Object Oriented Programming in PHP

Page 2: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

List of Items of Interest• What is a Class• What is an Object• What is a Singleton• Multiple Objects, How to store.• Advantages and Disadvantages of OOP• Parts of a Class

– Properties– Methods– Constructors

• Creating an Object• Passing data to an object when it is first created or to the constructor• Passing data to a property• Passing data to a method• Getting data from a property• Getting data from a method• Naming issues/requirements• Printing• PDO vs mysql_link• Sending multiple variables of data back from a method• Parsing of the returned data

Page 3: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

What is a Class

• A Class is a template of code that is used to create an Object.

• Objects do the work, Classes are the design of the code to do the work.

Page 4: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

What is an Object

• An Object is a Instantiation of a Class.• If you have only one Object from a class it is

called a Singleton• If you have multiple Objects from a class it is

best to store the objects in an array.

Page 5: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Advantages and Disadvantages of OOP

• If you have a large project in PHP or if you have a project that is in a constant state that it is running (other then PHP) then you should use OOP.

• If you have a small project that does not use a lot of reused code then Procedural programming is better as it is lighter.

Page 6: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Parts of a Class

• Properties– If you have variables that are to be shared between

methods it is best to declare them as properties.– As of PHP5 properties start with private, public or

protected– The visibility of private, public or protected is different

then ActionScript– public $name = “Doug”;– They are reference with $this->name in methods– They are references from the main file with

• $oObject->name ;

Page 7: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Parts of a Class

• Methods– Methods are functions that work on data.– As of PHP5 methods start with private, public or

protected– The visibility of private, public or protected is different

then ActionScript– public function myToDo () { }– They are reference with $this->myToDo () in other

methods– They are referenced from the main file with

• $oObject->myToDo () ;

Page 8: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Parts of a Class

• Constructors– Constructors are special methods that are called

when an Object is created.– As of PHP5 Constructors are named as

__construct()

– The visibility of private, public or protected is different then ActionScript

– public function __construct () { }– I usually use the constructor to set default properties

for the object such as connecting to the Database.

Page 9: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Creating an Object

• You first need to include the Class with a require_once to reference the file the class is in.

• I always start the name of my objects with a lowercase o and then camel case the rest of the name.$oObject

• Use the new command to create the object$oObject = New Bicycles();

Page 10: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Passing data to an object when it is first created or to the constructor

• When creating the object from the class pass variable data in the brackets to the constructor.

• $oObject = New Bicycles($id);

-------------------------

Class Bicycles {

public $id = “”;

public function __construct ($id) {

$this->id = $id;

}

}

Page 11: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Passing data to a property

$oObject->id = $id; // outside object$oObject->id = “22”; // outside object

$this->id = $id; // from within object$this->id = “22”; // from within object

Page 12: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Passing data to a method

• Data id passed in the brackets of the method name;

$oObject->getBicycle($id); // outside object$oObject->getBicycle(“22”); // outside object

$this->getBicycle($id); // from within object$this->getBicycle(“22”); // from within object

Page 13: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Getting data from a property

$id = $oObject->id ; // outside objectprint $oObject->id ; // outside object

$id= $this->id; // Do not do this as you just need to reference the property only from within object

Page 14: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Getting data from a method

$title = $oObject->getBicycleTitle($id); // outside object

print $oObject->getBicycleTitle($id); // outside object

$title = $this->getBicycleTitle($id); // from within object

(Never Print from with in a Method except for debugging)

Page 15: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Naming issues/requirements

• When naming properties and methods use camel case

• When naming variables use lowercase with underscores.

• The use of objects reduces name space issues• Start all class names with a capital• Start all objects with a lower case o

Page 16: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Printing

• NEVER use print or echo with a class or object• (printing can only be used for debugging)• It is bad form• data does not print where it needs to be seen.

Page 17: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

PDO vs mysql_link

• The procedural way of connecting to a database is to create a resource link such as $mysql_link

• For OOP use the PDO way which is PHP Database Objects• Create a property in the __construct that has the reference to the PDO.

public $pDbh ;

// define constructorpublic function __construct(){

require_once("connect_dbh.php");$this->pDbh = &$dbh;

}

Page 18: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

PDO vs mysql_linkconnect_dbh.php

<?php

try{ $user = ”youraccount"; $pass = ”yourpassowrd"; $dbh = new PDO('mysql:host=localhost;dbname=yourdatabase', $user, $pass);}catch (PDOException $e){ print("Error: ".$e->getMessage()."<br /> ");}

?>

Page 19: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

PDO vs mysql_linkSELECT

$query = "SELECT titleFROM bicyclesWHERE id = '$id'";

foreach ($this->pDbh->query($query) as $row){

$title = stripslashes($row[0]);}

Page 20: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

PDO vs mysql_linkINSERT, DELETE or UPDATE

$query = ”UPDATE bicyclesSET title = ‘$title’WHERE id = '$id'";

$this->pDbh2->query($query);

Page 21: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Sending multiple variables of data back from a method

• You can use a return command to send a single variable back from the calling method.

public function addThis ($a,$b) {

$total = $a + $b;return $total;

}

Page 22: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Sending multiple variables of data back from a method

• To send multiple variables back in a return you need to place the variables into an array.

public function getBicyclesList () {

$data = “”;$query = "SELECT id, titleFROM bicyclesORDER BY id ”;$x = 0;foreach ($this->pDbh->query($query) as $row){$id= stripslashes($row[0]);$title = stripslashes($row[1]);$data[$x][‘id’] = $id;$data[$x][‘title’] = $title;++$x;}

return $data;}

Page 23: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Parsing of the returned data

• If the data is a simple structure then just print it or place into a variable.print $oObject->addThis(12, 6) ; // prints 18

Page 24: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

Parsing of the returned data

• If the data is an array structure then you need to parse the array.

$bikeListArray = $oBicycles->getBicyclesList();

foreach ($bikeListArray as $key=>$bike) {

$id = $bike[‘id’];$title = $bike[‘title’];print(“<option value=\”$id\”>$title</option>”);

}

Page 25: Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages

File Names

• Each class resides in it’s own file• Each file has the name class in it.• The file begins with the name of the class bicycles.class.phpThese files will reside in the inc/php folder