 |
|
ss
Oracle Tips by Burleson |
Inheritance
In the real world, data types are frequently
related to each other in a hierarchic way. What does this mean? Both
cattle and poultry are domestic animals. Chicken and turkeys are
poultry, horses and cows are herbivores. Managers are employees, but
not all employees are managers. A hierarchy may look like this:
In the OO world arrows are represented by
inheritance. The most general class in the above diagram is the class
“Domestic Animal”. Class “Herbivore” inherits all the properties of
the domestic animals and adds additional ones. To introduce some OO
terminology, it is sometimes said that class “Domestic Animal” is a
client class for the class “Employee” or that class “Herbivore”
specializes class “Domestic Animal”.
This book will use terms “client class” and
“inherits from” interchangeably. Objects of the client class are also
the objects of the parent class because the client class inherits all
the properties from the parent class and adds some new. Every manager
is also an employee. Inheritance is a hierarchical relationship which
can be described by the phrase “is a” as in “manager is an employee”.
In C++ it is possible for a class to inherit from
multiple classes, but not so in PHP. A PHP class can have only one
base class. True to the form, another example is constructed to
explain inheritance and related topics in PHP:
#!/usr/local/bin/php
<?php
class employee {
private static$counter=1;
protected $ename;
protected $empno;
function __construct($ename) {
$this->empno=self::$counter++;
$this->ename=$ename;
}
function show_emp(){
echo $this->empno."\t".$this->ename."\n";
}
}
class manager
extends employee {
private $manages;
function __construct($ename) {
parent::__construct($ename);
$this->manages=array();
}
function manage($empno) {
$n=array_push($this->manages,$empno);
}
function show_emp(){
echo $this->ename.” manages ";
foreach ($this->manages as $m) {
print " $m";
}
print "\n";
}
}
$a=new employee("Tom");
$a->show_emp();
$b=new employee("Dick");
$b->show_emp();
$c=new employee("Harry");
$c->show_emp();
$d=new manager("Larry");
$d->manage(1);
$d->manage(2);
$d->manage(3);
$d->show_emp();
?>
Here there are two classes: employee and
manager. The class manager inherits from the class
employee. There are several subtleties that need to be explained
in this example, but one should see what happens when the example is
executed first:
$
./example12.php
1 Tom
2 Dick
3 Harry
Larry manages 1 2 3
The variable $d represents a manager named
Larry. For managers, the show_emp()
function lists employees managed by them, while for ordinary employees
(Tom, Dick and Harry) the show_emp() function simply shows the
employee number and the employee name. This is called method
overriding. The show_emp() method from the class manager
overrides the show_emp() method from the class employee.
It is worth noticing that the show_emp()
method in the manager class uses the $ename member which
belongs to the parent class. This is the illustration of how to use
protected members. They can be accessed from the client class.
The client class explicitly calls the constructor
of the parent class by using “parent::__construct()”. PHP, in
contrast to other OO languages, does not automatically call a
constructor of the parent class when the constructor in the client
class is invoked. If a manager is to be constructed as a proper
employee, the constructor has to be explicitly called.
The class employee contains a “private static”
member $counter, which is initialized to 1. The keyword
static has been met in the context of function variables retaining
values between the invocations, but not in the class context. In the
OO context, static variables belong to the class itself and not to any
object of that class. This means that the static method cannot be
accessed using $thisbecause
$this points to the current object to which static members do
not belong.
Static methods are also available which, in
complete analogy with the static
members, cannot reference $this. In order to access the class
of the object, the keyword self is used, which means “the class
of the object”. Operator “::” is so called “scope resolution
operator”. It is used to access static members, constants and
overridden methods in the parent class.
If the following method is added to the class
manager, it will produce the desired results:
function
emp_show () {
parent::show_emp();
}
Calling $d->emp_show() will invoke the show_emp()method of the parent class.
See
code depot for complete scripts
The above book excerpt is from:
Easy Oracle
PHP
Create Dynamic Web Pages with Oracle Data
ISBN
0-9761573-0-6
Mladen Gogala
http://www.rampant-books.com/book_2005_2_php_oracle.htm
|