Download PowerPoint File, 367 KB

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Dynamic Internet Technologies
PHP – object oriented
programming in web
development 2: inheritance
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
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
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 "<p>Fuel: $this->fuelLevel</p>/n";
}
}
vehicle
aeroplane
boat
car
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 "<p>Fuel: $this->fuelLevel</p>/n";
}
}
class aeroplane extends vehicle {
}
$airbus1 = new aeroplane();
$airbus1->getFuelLevel();
Child object
of vehicle
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 "<p>Fuel: $this->fuelLevel</p>/n";
}
}
class aeroplane extends vehicle {
public function lowerLandingGear() {
echo "<p>Lowering ...</p>/n";
}
}
$airbus1 = new aeroplane();
$airbus1->getFuelLevel();
$airbus1->lowerLandingGear();
Extending a web page class example

If we had a web page
class that served as a
basic blueprint for
creating web pages
with



What specialisations
could the child
classes have?
A header, body and
footer etc
We could create child
classes to extend it
webPage
with menu
Different
DOCTYPE
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 "<p>Fuel: $this->fuelLevel</p>/n";
}
}
class aeroplane extends vehicle {
public function getFuelLevel() {
// if low send SOS to air
The child
// traffic control!
method is
called in
}
preference
}
to the
$airbus1 = new aeroplane();
parent’s of
$airbus1->getFuelLevel();
the same
name
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 "<p>Fuel: $this->fuelLevel</p>/n";
}
}
class aeroplane extends vehicle {
public function getFuelLevel() {
// if low send SOS to air traffic control!
parent::getFuelLevel();
}
}
$airbus1 = new aeroplane();
$airbus1->getFuelLevel();
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
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)
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
0-7645-4166-8
Welling, L. and Thompson, L. (2005). PHP and MySQL
Web Development. Third Edition. Sams Publishing
http://www.php.net
http://www.w3schools.com/PHP/default.asp