PHP: Other Information about Class & OOP

person Jason Huangfolder_openCode, PHPaccess_time November 4, 2009

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.

Comments

  1. It is the best time to make some plans for the future and it is time to be happy.. I have read this post and if I could I desire to suggest you few interesting things or advice… Maybe you could write next articles referring to this article… I want to read even more things about it! Roofing of Fort Worth, 8100 Wallace Road, Fort Worth, TX, 76135, US, 817-330-8100

  2. Simply wish to say your article is as astonishing.
    The clarity in your submit is just spectacular and that i can assume you’re knowledgeable on this subject. Fine together with your permission allow me to grasp your RSS feed to keep updated with imminent post. Thank you 1,000,000 and please carry on the gratifying work.

warningComments are closed.