Quantcast
Channel: Monkey Can Code » PHP
Viewing all articles
Browse latest Browse all 5

PHP: Other Information about Class & OOP

$
0
0

Interfaces
In large object-oriented projects, there is some advantage to be realized in having standard
names for methods that do certain work. For example, if many classes in a software applica-
tion needed to be able to send e-mail messages, it would be desirable if they all did the job
with methods of the same name. As of PHP5, it is possible to define an interface, like this:

interface Mail {
public function sendMail();
}
Then, if another class implemented that interface, like this:
class Report implements Mail {
// Definition goes here
}
it would be required to have a method called sendMail. It’s an aid to standardization

Abstract Classes
An abstract class is one that cannot be instantiated, only inherited.

You declare an abstract class with the keyword abstract, like this:
abstract class MyAbstractClass {
abstract function myAbstractFunction() {
}
}
Note that function definitions inside an abstract class must also be preceded by the keyword
abstract. It is not legal to have abstract function definitions inside a non-abstract class.

Some other OOP languages make a distinction between instance member variables, on the
one hand, and class or static member variables on the other. Instance variables are those that
every instance of a class has a copy of (and may possibly modify individually); class variables
are shared by all instances of the class. Similarly, instance functions depend on having a par-
ticular instance to look at or modify; class (or static) functions are associated with the class
but are independent of any instance of that class.
In PHP, there are no declarations in a class definition that indicate whether a function is
intended for per-instance or per-class use. But PHP does offer a syntax for getting to func-
tions in a class even when no instance is handy. The :: syntax operates much like the -> syn-
tax does, except that it joins class names to member functions rather than instances to
members. For example, in the following implementation of an extremely primitive calculator,
we have some functions that depend on being called in a particular instance and one function
that does not:
class Calculator
{
var $current = 0;
function add($num) {
$this->current += $num;
}
function subtract($num) {
$this->current -= $num;
}
function getValue() {
return($current);
}
function pi() {
return(M_PI); // the PHP constant
}
}

We are free to treat the pi() function as either a class function or an instance function and
access it using either syntax:
$calc_instance = new Calculator;
$calc_instance->add(2);
$calc_instance->add(5);
print(“Current value is “ .
$calc_instance->current .”<BR>”);
print(“Value of pi is “ .
$calc_instance->pi() . “<BR>”);
print(“Value of pi is “ .
Calculator::pi() . “<BR>”);
This means that we can use the pi() function even when we don’t have an instance of
Calculator at hand. The Calculator class has to be accessible in either case, though,
meaning it has to have been imported with a require_once statement, or something similar.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images