Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Classes 101

Justin Yost
November 04, 2014

PHP Classes 101

Introduction to PHP Classes and Objection Oriented Programming in PHP.

Justin Yost

November 04, 2014
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. 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
  2. What is a Class —Class is an implementation of an

    Object —So a Class defines the properties and methods of an Object
  3. Examples? —These can be instances of real world objects; Car,

    Bike, Book, etc —Or virtual objects; Shopping Cart, HTTP Request, etc
  4. Using a Class include 'vehicle.php'; $Vehicle = new Vehicle(); //

    echos the $speed value for the current instance of $Vehicle echo $Vehicle->getSpeed();
  5. Slightly more complex class class Vehicle { var $speed =

    0; function getSpeed() { return $this->speed; } function setSpeed($speed = 0) { $this->speed = $speed; } }
  6. 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
  7. 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
  8. Extend a Class class Car extends Vehicle { var $numberOfWheels

    = 4; function getNumberOfWheels() { return $this->numberOfWheels; } }
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. Constructor Code class Vehicle { public $name = null; public

    function __construct($nameOfVehicle = null) { $this->name = $nameOfVehicle; } public function getName() { return $this->name; } }
  17. Constructor Code $VehicleA = new Vehicle(); $VehicleB = new Vehicle('testName');

    echo $VehicleA->getName(); // echoes null echo $VehicleB->getName(); // echoes 'testName'
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. class Vehicle { public $owner; public $registrationNumber; } class Scooter

    extends Vehicle { } class Car extends Vehicle { public $isPassedSmogCheck = false; } class Truck extends Car { public $towingWeightLimit; }
  25. 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
  26. 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
  27. 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