Slide 1

Slide 1 text

PHP Classes Justin Yost Web Developer at Loadsys

Slide 2

Slide 2 text

What is Object Oriented Programming (OOP)? —Creating Objects which have both methods and properties —Use these Objects together to create and build your overall program —These Objects are instances of Classes

Slide 3

Slide 3 text

What is a Class —Class is an implementation of an Object —So a Class defines the properties and methods of an Object

Slide 4

Slide 4 text

Examples? —These can be instances of real world objects; Car, Bike, Book, etc —Or virtual objects; Shopping Cart, HTTP Request, etc

Slide 5

Slide 5 text

When To Use Classes —Simplify

Slide 6

Slide 6 text

When To Use Classes —Group

Slide 7

Slide 7 text

When To Use Classes —Order/Structure

Slide 8

Slide 8 text

Basic PHP Class: class Vehicle { }

Slide 9

Slide 9 text

Property: class Vehicle { var $speed = 0; }

Slide 10

Slide 10 text

Methods: class Vehicle { var $speed = 0; function getSpeed() { return $this->speed; } }

Slide 11

Slide 11 text

Using a Class include 'vehicle.php'; $Vehicle = new Vehicle(); // echos the $speed value for the current instance of $Vehicle echo $Vehicle->getSpeed();

Slide 12

Slide 12 text

Slightly more complex class class Vehicle { var $speed = 0; function getSpeed() { return $this->speed; } function setSpeed($speed = 0) { $this->speed = $speed; } }

Slide 13

Slide 13 text

Using More Complex Class include 'vehicle.php'; $Vehicle = new Vehicle(); $Vehicle->setSpeed(10); echo $Vehicle->getSpeed(); // echoes 10 $Vehicle->setSpeed(20); echo $Vehicle->getSpeed(); // echoes 20

Slide 14

Slide 14 text

Multiple Instances include 'vehicle.php'; $Vehicle_A = new Vehicle(); $Vehicle_B = new Vehicle(); $Vehicle_A->setSpeed(10); echo $Vehicle_A->getSpeed(); // echoes 10 $Vehicle_B->setSpeed(20); echo $Vehicle_B->getSpeed(); // echoes 20

Slide 15

Slide 15 text

Extend a Class class Car extends Vehicle { var $numberOfWheels = 4; function getNumberOfWheels() { return $this->numberOfWheels; } }

Slide 16

Slide 16 text

Multiple Instances include 'vehicle.php'; include 'car.php'; $Vehicle = new Vehicle(); $Car = new Car(); $Vehicle->setSpeed(10); echo $Vehicle->getSpeed(); // echoes 10 $Car->setSpeed(20); echo $Car->getSpeed(); // echoes 20 echo $Car-> getNumberOfWheels(); // echoes 4

Slide 17

Slide 17 text

Inheritance —Notice our Car Class inherits all the properties and methods in Vehicle —And we can extend Car to do more —We can then extend Car to Truck or Sedan, etc —Or extend Vehicle to GoCart or Scooter, etc —Inheritance is one of the key concepts behind OOP

Slide 18

Slide 18 text

Visibility —Classes can define the visibility of a method or property: —public - can be accessed from everywhere —protected - only accessed within the class itself and by inherited/parent classes —private - only accessed within the class itself

Slide 19

Slide 19 text

Public Visibility class Vehicle { public $test = "test"; public function publicReturn() { return $this->test; // works } } class Car extends Vehicle { public function testCar() { return $this->test; // works } } $Vehicle = new Vehicle(); $Vehicle->test // works $Vehicle->publicReturn() // works

Slide 20

Slide 20 text

Protected Visibility class Vehicle { protected $test = "test"; protected function publicReturn() { return $this->test; // works } } class Car extends Vehicle { protected function testCar() { return $this->test; // works } } $Vehicle = new Vehicle(); $Vehicle->test // fails $Vehicle->publicReturn() // fails

Slide 21

Slide 21 text

Private Visibility class Vehicle { private $test = "test"; private function publicReturn() { return $this->test; // works } } class Car extends Vehicle { private function testCar() { return $this->test; // fails } } $Vehicle = new Vehicle(); $Vehicle->test // fails $Vehicle->publicReturn() // fails

Slide 22

Slide 22 text

Constructors —PHP has some magic methods for Classes, including the one you'll probably use the most: —__construct —__construct Takes any number of arguments passed when "constructing" a new instance of the class

Slide 23

Slide 23 text

Constructor Code class Vehicle { public $name = null; public function __construct($nameOfVehicle = null) { $this->name = $nameOfVehicle; } public function getName() { return $this->name; } }

Slide 24

Slide 24 text

Constructor Code $VehicleA = new Vehicle(); $VehicleB = new Vehicle('testName'); echo $VehicleA->getName(); // echoes null echo $VehicleB->getName(); // echoes 'testName'

Slide 25

Slide 25 text

Little Tangent into Software Engineering SOLID Principles Of Object Oriented Programming 1. Single responsibility principle 2. Open/closed principle 3. Liskov substitution principle 4. Interface segregation principle 5. Dependency inversion principle

Slide 26

Slide 26 text

Single responsibility principle —A Class has a single responsibility. —Only one change in the specification of your project should affect the specification/design of the class —So if you change speed from being an integer to a decimal value it should only affect the Vehicle class not everything else in your program

Slide 27

Slide 27 text

Open/closed principle —A Class should be open for extension but closed to modification —Classes can be changed without modifying the source code of the original file —So we can use Inheritance to modify what a class does without editing the original file. —Do you need Vehicles to store more than just the speed or name, extend Vehicle and make the changes there

Slide 28

Slide 28 text

Liskov substitution principle —Sometimes called Design by Contract —Replacing an instance of a class with an instance of a different subtype should not change the correctness of the program —If we have lines of code that calls for an instance of a Vehicle, providing an instance of Car will not change the correctness of the code

Slide 29

Slide 29 text

Dependency inversion principle —Pieces of software should be decoupled and not dependent upon low level implementation details —How we talk to the database or an API is a low level details —Our code should simply talk to a middle layer that deals with the details of talking to the specific database or API —Most ORM's use PDO to abstract away the details of if we are talking to MySQL or MSSQL or

Slide 30

Slide 30 text

Crude Test Case Build a System to Store the Details of Vehicle Registrations —Multiple Types of Vehicles with Different Registration Details —Cars and Trucks and Scooters —All require an owner and the registration number —Cars and Trucks Require Smog Testing —Trucks Require Towing Weight Limit

Slide 31

Slide 31 text

class Vehicle { public $owner; public $registrationNumber; } class Scooter extends Vehicle { } class Car extends Vehicle { public $isPassedSmogCheck = false; } class Truck extends Car { public $towingWeightLimit; }

Slide 32

Slide 32 text

Simplify —We only have to write the code to set the owner and registration number once —But we get to use it three other places —Imagine we had to also ensure we wanted to set the max speed of a vehicle and be able to set it and return it as either MPH or KMPH or some other ratio

Slide 33

Slide 33 text

Group —We grouped the logic for each piece in one central location —We could make this even better by for instance creating a new class for storing names to not have to deal with the crazy-ness of names in our Vehicle class

Slide 34

Slide 34 text

Order/Structure —Classes bring a sense of hierarchy and structure to our project —Problems get at the point closest to where they actually exist —If we need to add in a way to allow for an exemption to the smog check, it goes exactly where the problem would occur and at no earlier or later point

Slide 35

Slide 35 text

Questions? Justin Yost Web Developer at Loadsys —github.com/jtyost2 —twitter.com/jtyost2 —yostivanich.com