Object-oriented programs are built through a hierarchical structure of classes. In single inheritance languages such as PHP, class inheritance is tree-like. A root class has one or more subclasses, and then one or more subclasses are inherited from each subclass. More next-level subcategories. Of course, there may be multiple root classes used to implement different functions. In a well-designed architecture, each root class should have a useful interface that can be used by application code. If our application code is designed to work with the root class, it can also work with any subclass that inherits from the root class.
An abstract method is just like a placeholder for a regular method in a subclass (it takes up space but has no effect), but unlike a regular method it doesn't have any code. If there are one or more abstract methods in a class, then the class becomes an abstract class. You cannot instantiate abstract classes. You must inherit from them and then instantiate subclasses. You can also think of abstract classes as a template for subclasses.
If you override all abstract methods, the subclass becomes a normal class. If all methods are not overridden, the subclass is still abstract. If a class contains an abstract method (even if there is only one), you must declare the class to be abstract by adding abstract before the class keyword.
The syntax of declaring an abstract method is different from that of declaring a general method. The abstract method does not have the main body contained in braces {} like the general method, and ends with a semicolon;.
In Example 6.13, we define a class Shape that contains a getArea method. But since it is impossible to determine the area of the figure without knowing the shape, we declared the getArea method as an abstract method. You cannot instantiate a Shape object, but you can inherit from it or use it in an expression, as in Example 6.13.
If you create a class with only abstract methods, you define an interface. To illustrate this situation, PHP has the interface and implements keywords. You can use interface instead of abstract classes and implements instead of extends to describe your class definition or use an interface. For example, you can write a myClass implements myIterface. You can choose between these two methods based on personal preference.
/*Note:
The two methods refer to:
1. abstract class aaa{} (note that there are only abstract methods in aaa, no general methods)
class bbb extends aaa{} (override the abstract method in aaa in bbb)
2. interface aaa{}
class bbb implements aaa{} (override the abstract method in aaa in bbb)
*/
Listing 6.13 Abstract classes
//abstract root class abstract root class
abstract class Shape
{
abstract function getArea(); //Define an abstract method
}
//abstract child class abstract subclass
abstract class Polygon extends Shape //Polygon
{
abstract function getNumberOfSides();
}
//concrete class entity class triangle class
class Triangle extends Polygon
{
public $base;
public $height;
public function getArea() //Override the area calculation method{
return(($this->base * $this->height)/2);
}
public function getNumberOfSides() //Override side count method{
return(3);
}
}
//concrete class entity class quadrilateral
class Rectangle extends Polygon
{
public $width;
public $height;
public function getArea()
{
return($this->width * $this->height);
}
public function getNumberOfSides()
{
return(4);
}
}
//concrete class entity class circle
class Circle extends Shape
{
public $radius;
public function getArea()
{
return(pi() * $this->radius * $this->radius);
}
}
//concrete root class defines a color class
classColor
{
public $name;
}
$myCollection = array(); //Create a collection of shapes and put it into an array
//make a rectangle
$r = new Rectangle;
$r->width = 5;
$r->height = 7;
$myCollection[] = $r;
unset($r);
//make a triangle
$t = new Triangle;
$t->base = 4;
$t->height = 5;
$myCollection[] = $t;
unset($t);
//make a circle
$c = new Circle;
$c->radius = 3;
$myCollection[] = $c;
unset($c);
//make a color
$c = new Color;
$c->name = "blue";
$myCollection[] = $c;
unset($c);
foreach($myCollection as $s)
{
if($s instanceof Shape) //If $s is an instance of Shape class{
print("Area: " . $s->getArea() . "n");
}
if($s instanceof Polygon)
{
print("Sides: " .$s->getNumberOfSides()."n");
}
if($s instanceof Color)
{
print("Color: $s->name n");
}
print("n");
}
?>