Abstract class for PHP

Source: Internet
Author: User

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.
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.