Section 10 - Abstract Method and Abstract Class - Classes and Objects in PHP5 [10]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Section 10 - Abstract method and abstract class The object program is built by the hierarchical structure of the class. In a single inheritance language such as PHP, the inheritance of the class is a tree. A root class has one or more subclauses, and then inherits one or more from each subclass. There are many subclasses. Of course, there may be multiple roots, used to achieve different functions. In a well design system, each root class should have a useful interface, which can be used by the application code. If our application code is designed to work with the root class, it can also cooperate with any subclasses inherited from the root class. Abstract method is a placeholder (occupied by a place) in a subclass But don't work), it is different from the general method - there is no code. If there is one or more abstract methods in the class, then this class will become an abstract class. You can't instantiate the abstract class. You must inherit them, then instance Class. You can also view the abstraction class as a template for subclasses. If you override all abstract methods, subclasses become an ordinary class. If you don't overwrite all methods, subclasses are still abstract If a class contains an abstract method (even one), you must declare this class is abstract, plus Abstract before the Class keyword. The syntax of the declaration abstraction method is different. Abstract method Like the general method, the main body part is included in the braces {}, with a semicolon; in Example 6.13, we define a class shape containing the Getarea method. But because the shape of the pattern is not possible to determine the area of the graphic Self-adopting, we declare the getArea method as an abstract method. You can't instantiate a shape object, but you can inherit it or use it in an expression, just like this. If you have established a method Class, you define an interface (Interface). In this case, there is interface and imports keywords in PHP. You can use Interface instead of abstract classes, with Implements instead of Extends to illustrate your class definition or use an interface. For example, you can write a Myclass Implements MyIiterface. These two methods can be selected according to personal preferences. / * Note: Two methods refer to: 1. Abstract class aaa {} (Note Only Abstract Method in AAA, there is no general method) Class BBB Extends AAA {} (in BBB Abstract method over AAA) 2. Interface AAA {} Class BBB IMPLEMENTS AAA {} (Abstract Method in AAA in BBB) * / LISTING 6.13 Abstract Classes Php // Abstract Root Class Abstract Root Class Abstract class Shape {abstract function getArea (); // define an abstract method} // abstract child class abstract subclass of abstract class polygon extends Shape // polygon {abstract function getNumberOfSides ();} // concrete class triangle-based entity class triangle Extends polygon {public $ base; public $ heiGH;