Web Technology Solutions Class: OOP PHP, Design Patterns and CRUD Date : 02/09/2012
Tonight CRUD Redux Objects in PHP OOP Design Patterns Lab
Lab Preview Continue to build a simple CRUD app with the presidents table.
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 wheels: 4 color: white seats: 4 horsepower: 8 race car truck wheels: 4 color: white seats: 2 horsepower: 200 wheels: 4 color: red seats: 8 horsepower: 100 wheels: 4 color: white seats: 4 horsepower: 200 wheels: 4 color: white seats: 4 horsepower: 200
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 Encapsulation How 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 class Car { public function Car() { echo "We just created and object!"; } ?>
Class Constructor - PHP5 class Car { function __construct() { echo "We just created and object!"; } } ?>
Class Constructor - Properties class Car <?php { 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 4,7, and 14 See you Tuesday!