This article introduces the content is about PHP abstract class, has a certain reference value, now share to everyone, the need for friends can refer to
/*
The role of abstract classes:
When the interface is used, when the template is used
To achieve polymorphism, born to be a father
*/
= = = Code section 1===
= Write an aircraft manufacturing technique with an abstract =//
Abstract class Flyidea { //vigorously engine public abstract function engine (); Balance Rudder public abstract function balance (); /* Note: The abstract method cannot have the method body under this write is wrong public abstract function balance () {} error is as follows: Fatal error: Abstract function Flyidea::balance () cannot contain body */}/* abstraction class is not new to instantiate $kongke = new Flyidea (); error is as follows: Fatal error : Cannot instantiate abstract class flyidea*/
= Use Rocket to solve engine problem =//
Abstract class Rocket extends Flyidea {public function engine () { echo ' ignites gunpowder, loses balance. <br > '; }} Class Fly extends Rocket {public function engine () { echo ' force a throw <br > '; } Public function balance () { echo ' two paper wings remain balanced ~~~~~~ '; } Public Function Start () { $this->engine (); for ($i =0; $i <10; $i + +) { $this->balance (); echo ' Smooth flight <BR > ';}} } $apache = new Fly (); $apache->start ();
/*
Summarize:
Class is abstract class before adding
Method before adding abstract is a method of abstraction
Abstract classes cannot be instantiated
Abstract methods cannot have method bodies
There is an abstract method, then this class must be an abstract class
Abstract class, there may not be an abstract method inside
*/
= = = Code section 2===
=facebook Multi-lingual Welcome page =//
Abstract class Welcome {public abstract function wel ();} Class China extends Welcome {public function wel () { echo ' Hello, not dead <br > '; }} Class 中文版 extends Welcome {public function wel () { echo ' hi,welcome<br > '; }} Class Japan extends Welcome {public function wel () { echo ' search Dasnay <br > '; }} $c = ' China '; 中文版//japan$wel = new $c (); $wel->wel ();
/*
Other national languages have been added later,
Just add a new welcome class.
*/