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
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
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
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
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
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
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
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
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
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
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
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