Home >Backend Development >PHP Tutorial >Exploration of PHP5.0 Object Model: Abstract Methods and Abstract Classes_PHP Tutorial

Exploration of PHP5.0 Object Model: Abstract Methods and Abstract Classes_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:11:43728browse

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 system, 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 a placeholder just like a normal method in a subclass (it takes up a place but does not work). It is different from a normal method - it does not 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 a main body contained in braces {} like a 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 according to 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{} (in bbb Override the abstract method in aaa in bbb)
2. interface aaa{}
class bbb implements aaa{} (overwrite 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 the side counting 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
class Color
{
public $name;
}

$myCollection = array(); //Create a collection of shapes and put it into the 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");
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313902.htmlTechArticleObject-oriented programs are built through the hierarchical structure of classes. In single inheritance languages ​​such as PHP, class inheritance It is tree-like. A root class has one or more subclasses, and inherits from each subclass...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn