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