|
|
ss
Oracle Tips by Burleson |
Interfaces
and Abstract Classes
No discussion of OO concepts would be complete
without interfaces and abstract
classes. These notions exist in some form in all OO modern languages.
C++, Java and Python are the first ones that come to mind. There is a
noticeable absence of Perl from this chapter and this is because Perl
has never been designed to be an OO language. Those features were
added, but Perl OO facilities are a complicated patchwork added to the
language after the fact. Allegedly, Perl version 6 will have an OO
part redesigned from the ground up, just as PHP has redesigned its OO
part in version 5.
So, what are interfaces and abstractclasses? Interfacesand
abstract classes are an OO construction to force classes to implement
certain functions. Interface is simply a list of functions each class
that implements that interface must define. Here is an example:
interface emp
{
public function get_empno($name);
public function manage($empno);
}
A class can commit itself to implement those
functions by saying so in the class declaration:
class
employee implements emp {
....
}
Implementing interfaces is a weaker form of
inheritance in which no assumption on data members is made and no
parent class exists and no parent constructor can be called. No
assumption is made regarding the objects of the class that implements
an interface except the availability of the methods defined in the
interface.
Another form of inheritance is inheriting from an
abstractconcept. What is an
“abstract concept”? An abstract concept is a class that is too
abstract for creating actual objects of that class. For instance, the
class “domestic animal” is too general for having actual objects of
that class. Real domestic animals need more distinguishing
characteristics before objects of that class can be instantiated. Such
classes are called “abstract classes” and can only be used via an
inheritance mechanism. Abstract classes are marked by the keyword
“abstract”, as in the next example:
abstractclass A {
abstract function
fun1($arg1,$arg2);
abstract
function fun2($arg1);
public function fun3($arg1) {
....}
}
This class cannot be used directly in an expression like:
$x=new A;
Because class A is abstract,
this class can only be extended and the client class must implement
all functions that are marked as abstract. In the example above, any
class inheriting from class A must implement functions fun1 and
fun2, but does not have to implement the function fun3.
The function fun3 is passed down the inheritance chain.
So, abstract
classes are the classes that must be extended in order to be used, and
abstract functions are functions that must be overridden in order to
be used. Interestingly enough, there exists a direct opposite of
abstract classes and functions. A direct opposite of an abstract
class or a function is a final
class or function as the following declaration reveals:
finalclass A {
.......
}
Class A in this example cannot be extended. No class can
inherit from it. Similarly, a function can be declared “final”,
even if the class itself is not final. If the function show_emp()in example 12 is declared a final function, the class
declaration would look like the following:
class
employee {
private static$counter=1;
protected $ename;
protected $empno;
function __construct($ename) {
$this->empno=self::$counter++;
$this->ename=$ename;
}
final function show_emp()
{
echo $this->empno,"\t".$this->ename."\n";
}
}
The function show_emp() is now made “final”.
If an attempt to execute the example12.php with a similar
change, the following error will appear:
$
example12.php
PHP Fatal error: Cannot override finalmethod employee::show_emp()
in /home/mgogala/work/PHP/example12.php on line 35
$
The error message is self explanatory and reveals
that the functions declared as “final
cannot be altered.
The concepts of an interface, abstractclass and final class or function are rather general and
can be further refined by studying any other major OO language, such
as C++ or Java. As these things are fairly new in PHP, examples of
their use have not been found. The notion of the final function for
the next section is needed.
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
|