php--the difference between an abstract class and an interface

Source: Internet
Author: User

In the learning of PHP object-oriented, will be in the abstract class and interface confusion, the role of almost why it is so easy to confuse, why not leave one? But in fact the difference between the two is still very large, if you can use PHP two methods, object-oriented programming will be more reasonable, clear and efficient.

    • The use of the interface is implemented by keyword implements, and the operation of the abstract class is implemented using the keyword Exotends of the class inheritance, with special care.
    • Interfaces have no data members, but abstract classes have data members, and abstract classes can implement the encapsulation of data.
    • An interface has no constructors, and an abstract class can have constructors.
    • The methods in an interface are all public types, and methods in an abstract class can be decorated with private, protected, or public.
    • A class can implement multiple interfaces at the same time, but only one abstract class is implemented.

/* The use of the interface is implemented by the keyword implements, whereas the operation of the abstract class is inherited using the class
Keyword Exotends implementation, the use of special attention.
Interfaces have no data members, but abstract classes have data members, and abstract classes can implement the encapsulation of data.
An interface has no constructors, and an abstract class can have constructors.
The methods in an interface are all public types, and methods in an abstract class can be decorated with private, protected, or public.
A class can implement multiple interfaces at the same time, but only one abstract class is implemented.
*/


Interface person
{

Public function eat ();

Public function run ();

}

Class Mans implements Person
{
Public function Eat ()
{
Var_dump ("I'm eating something");
}

Public Function Run ()
{
Var_dump ("I'm Running");
}

}


$man =new Man ();

$man->eat ();

Abstract class Persionj
{

static public Function Eat ()
{
Var_dump ("Abstract class to eat again");
}

static public Function run ()
{
Var_dump ("Run Run");
}

}

Class woman extends Persionj
{

static public Function Eat ()
{
Parent::eat (); Todo:change the autogenerated stub
}

static public Function run ()
{
Var_dump ("The interface is running");
}

}

$woman =new woman ();

$woman->eat ();

Woman::eat ();

php--the difference between an abstract class and an interface

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.