Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP – object oriented programming in web development 2: inheritance Dynamic Internet Technologies.

Similar presentations


Presentation on theme: "PHP – object oriented programming in web development 2: inheritance Dynamic Internet Technologies."— Presentation transcript:

1 PHP – object oriented programming in web development 2: inheritance Dynamic Internet Technologies

2 Content Inheritance Extending parent classes to create more complex and specialised child classes that build on the basic features of the parent Overriding the methods of a parent class

3 Class inheritance The ability of one class definition (a child) to inherit all of the functionality of another (the parent) and to extend it When a child class inherits from a parent it Has all of the characteristics of its parent – e.g. its methods and properties Can add more complex and specialised features to provide additional or different functionality Can override (redefine) the behaviour of its parent

4 Inheritance example If we had a vehicle parent class what attributes and methods could it have? What kind of child classes could we have? class vehicle { protected $fuelLevel; protected $noSeats; protected $noWheels; public function getFuelLevel() { echo " Fuel: $this->fuelLevel /n"; } } vehicle caraeroplaneboat

5 Inheritance – child class ex Creating a child class that inherits all of the functionality of a parent class writing extends means that aeroplane inherits all the functionality of vehicle class vehicle { protected $fuelLevel; protected $noSeats; protected $noWheels; public function getFuelLevel() { // Code to determine and store level echo " Fuel: $this->fuelLevel /n"; } } class aeroplane extends vehicle { } $airbus1 = new aeroplane(); $airbus1->getFuelLevel(); Child object of vehicle

6 Inheritance – extending a parent class For our aeroplane child class example, what sort of specialised features could it have? The child class can use what it inherits and extend it with its own specialisations class vehicle { protected $fuelLevel; protected $noSeats; protected $noWheels; public function getFuelLevel() { echo " Fuel: $this->fuelLevel /n"; } } class aeroplane extends vehicle { public function lowerLandingGear() { echo " Lowering... /n"; } } $airbus1 = new aeroplane(); $airbus1->getFuelLevel(); $airbus1->lowerLandingGear();

7 Extending a web page class - example If we had a web page class that served as a basic blueprint for creating web pages with A header, body and footer etc We could create child classes to extend it What specialisations could the child classes have? webPage with menu Different DOCTYPE

8 Overriding the method of a parent class Child classes can override the methods of their parents Can be required to achieve some different behaviour Provide the child with a method of the same name as in the parent – thus redefining the method class vehicle { protected $fuelLevel; protected $noSeats; protected $noWheels; public function getFuelLevel() { echo " Fuel: $this->fuelLevel /n"; } } class aeroplane extends vehicle { public function getFuelLevel() { // if low send SOS to air // traffic control! } } $airbus1 = new aeroplane(); $airbus1->getFuelLevel(); The child method is called in preference to the parent’s of the same name

9 Overriding the method of a parent class The overridden function can still call the parent function using… class vehicle { protected $fuelLevel; protected $noSeats; protected $noWheels; public function getFuelLevel() { echo " Fuel: $this->fuelLevel /n"; } } class aeroplane extends vehicle { public function getFuelLevel() { // if low send SOS to air traffic control! parent::getFuelLevel(); } } $airbus1 = new aeroplane(); $airbus1->getFuelLevel();

10 Overriding - web page class example How could we use overriding in child classes of our webPage class parent? Examples Override the makeHeader method to produce a web page with a different DOCTYPE Override the footer method to create a more complex footer Override the constructor to accept additional parameters like text / image file names etc that the page uses Redefine any method to change functionality

11 Review We can use inheritance to Extend parent classes to create more complex and specialised child classes that build on the basic features of the parent Overriding the method of a parent class to help achieve some more complex or specialised behaviour. There is a principle in OO that the “local” class member is preferred (very important to OO programming)

12 References Coggeshall, J. (2005). PHP 5. Sams Publishing. Lerdorf, R. & Tatrow, K. (2005). Programming PHP. O’Reilly Sklar, D. (2004). Learning PHP 5. O’Reilly Valade J. (2004). PHP 5 for Dummies. Wiley. ISBN Welling, L. and Thompson, L. (2005). PHP and MySQL Web Development. Third Edition. Sams Publishing


Download ppt "PHP – object oriented programming in web development 2: inheritance Dynamic Internet Technologies."

Similar presentations


Ads by Google