date : 02/05/2012 web technology solutions class: oop php, design patterns and crud

30
Date : 02/05/2012 Web Technology Solutions Class: OOP PHP, Design Patterns and CRUD

Upload: louise-doyle

Post on 02-Jan-2016

229 views

Category:

Documents


1 download

TRANSCRIPT

Date : 02/05/2012

Web Technology SolutionsClass: OOP PHP, Design Patterns and CRUD

Tonight

✤ CRUD Redux

✤ Hands On Functions\Arrays

✤ Objects in PHP

✤ OOP Design Patterns

✤ Lab

Lab Preview

✤ Continue to build a simple CRUD app with the presidents table.

CRUD ReviewCreate, Read, Update and Delete. How To, Tips, Tricks

Array and Function Challenges4 quick challenges to test your programming skills.

Array Challenge #1

$arr = array("name", "age", "race", "job");Search through this array and find the item “race” and return the key of that item.

Array Challenge #2Find only what is different in these two arrays and return those unique elements.$array1 = array(1 => "WA", "CA", "UT", "MT");$array2 = array(2 => "CA", "MT", "UT");

Function Challenge #1Create a custom function that takes two array’s and combines those arrays into one array and return a single array.$array1 = array(1 => "WA", "CA", "UT", "MT");$array2 = array(2 => "VA", "NV", "OR");

Function Challenge #2With the following string, break up each word and put the contents of each word into an array. Then echo out the contents of that array.$string = “Tomato Sausage Cheese Peppers Mushrooms”;

OOP in PHP

✤ Object Oriented Programming

✤ Objects and Classes

✤ PHP4 vs PHP5

✤ Design Patterns

✤ Example

OOP in PHP

✤ Objects are discrete bundles of functions relating a concept (database connections, image creation, managing users)

✤ Objects work together to create applications.

✤ Service multiple projects with a single set of code.

✤ Code is portable, encapsulated, reusable and easier to debug.

OOP Example

OOP Car

✤ Functions of a car

✤ Accelerate, Stop, Steering

✤ How? (implementation)

✤ Gas Pedal, Engine

✤ Breaks

✤ Steering and Wheels

✤ Care how it works? No.

Object Oriented Programming

What is an object:

✤ A “copy” or “instance” of a class.

✤ Objects have properties (color, speed, horsepower, seats)

✤ Objects have methods (accelerate, stop, steering).

Object Oriented Programming

<?php

/*

* File Name: car

* Date: November 18, 2010

* Author: Lincoln Mongillo

* Description: Manages Car

*/

class Car

{

}

?>

Object Oriented Programming

<?php

$myRaceCar = new Car();

$myRaceCar -> accelerate(55);

?>

Object Oriented Programming

Car = a basic blueprint that represents any car.

color

wheels

seats

horsepower

With Car we can “create” other types of speciality cars: dump trucks, sports cars. How? Classes can “inherit” properties and methods from other classes.

Object Oriented Programming

Car

racecar

truck

wheels: 4color: whiteseats: 4horsepower: 8

wheels: 4color: whiteseats: 2horsepower: 200

wheels: 4color: whiteseats: 4horsepower: 200

wheels: 4color: whiteseats: 4horsepower: 200

wheels: 4color: redseats: 8horsepower: 100

Object Oriented Inheritance

<?php

/*

* File Name: RaceCar

* Date: November 18, 2008

* Author: Lincoln Mongillo

* Description: Manages RaceCars

*/

class RaceCar extends Car

{

}

?>

Object Oriented Polymorphism

A Truck vs. Race Car come from car.

Extending a class allows for “Polymorphism” which allows you to perform different tasks based on the context you’re using it in.

Example: stop() between truck vs. racecar is somewhat the same but different because of the nature of the truck (size) and racecar (speed). It might implement the method differently to get the result.

Object Oriented Encapsulation

Don’t need to know how it works (members functions or props)

You just need to know what you can do (available methods - interface)

Object Oriented EncapsulationHow do you know what you can use?

Levels of visibility

• public: means that a class member is visible and usable / modifiable by everyone - default in PHP

• private: means that a class member is only usable / modifiable by the class itself

• protected: means that a class member is only usable / modifiable by the class itself and eventual sub-classes

Class Constructor - PHP4

<?php

class Car

{

public function Car() {

echo "We just created and object!";

}

}

?>

Class Constructor - PHP5

<?php

class Car

{

    function __construct() {  

        echo "We just created and object!";  

    }  

}

?>

Class Constructor - Properties

<?php

class Car

{

private $color;  

private $wheels;  

private $seats;  

    function __construct() {  

$this->color = “OxFFFFF”;

$this->wheels = 4;

    }  

}

?>

Class Constructor - Properties

    function __construct() {  

$this->color = “OxFFFFF”;

$this->wheels = 4;

 

// $this is a reference to the object that is calling it.

$truck = new Car(); // $this = truck.

Object Oriented Programming

Image Example

Database Example

Lab & Next Week

✤ Please send me your Survey in word doc format.

✤ Your personal DB will be updated next week and we will start working on the app.

Lab & Next Week

✤ Homework

✤ Complete Presidents CRUD system

Lab & Next Week

✤ Next week we will

✤ Create Registration system

✤ Create Login w\ Encrypt Password.

✤ Security and Authorization

✤ Reading: Chapter 8

See you Tuesday!