PHP abstraction and interface differences [reprint]

Source: Internet
Author: User

From: http://www.cnblogs.com/k5054/archive/2012/12/26/2834205.html

For object-oriented development, abstract classes and interfaces these two things are more difficult to understand!

I've been watching all day today! Summarize it yourself!

abstract class: is based on the class, which itself is a class, just a special class, can not be directly instantiated, it is possible to define methods in the class, properties! Similar to the template, the specification after the sub-class to achieve detailed functionality!

interface: The Main method-based specification, a bit like abstract methods in abstract class, but its relative to the abstract method, more independent! Allows a class to form new classes by combining multiple methods!

Abstract class is the same point as the interface:

1, are used to declare a certain thing, canonical name, parameters, the formation of modules, not detailed implementation details!

2, all through the class to achieve the relevant details work

3, syntax, abstract class abstraction method and interface, cannot have method body, namely {} symbol

4, can be inherited, interfaces can inherit the interface to form a new interface, abstract classes can inherit abstract classes to form a new abstract class

abstract class and interface. different points:

1, abstract class can have attributes, common methods, abstract methods, but interfaces can not have attributes, ordinary methods, can have constants

2, abstract class may not have an abstract method, but there must be an "abstract" method in the interface

3. There are different grammatical

1. Abstract class is declared with the keyword in the class, and the class is declared as classes, the interface is declared with interface, but cannot be declared by class, because the interface is not a class!

2, abstract class abstract method must be declared with an abstract, and the interface does not need

3, abstract class is to use the extends keyword to let subclasses inherit the parent class, in the sub-class implementation of the detailed abstract method! And the interface is to use implements to let the ordinary class in the class to implement the interface of the detailed method, and interface can be implemented at one time a number of methods, separated by commas each interface can be

Characteristics of each:

Abstract classes may not have abstract methods, but classes with abstract methods must be abstract classes

Within an abstract class , even if it is entirely concrete, it cannot be instantiated, as long as the new class inherits, the instance inherits the class to

interface allows one class to implement several different methods at once

The interface itself is abstract, but note that it is not an abstract class, because the interface is not a class, but its method is abstract! So, it's also abstract.

Application and combination:

The following code is based on their own thinking, not in the actual development of the application, but this kind of writing a bit peculiar! Let the abstraction and the interface together!

<?php
/*
I. The combination of abstract classes and interfaces

Write this program from your own guesses, want to implement an interface in an abstract class!
*/
Interface work{
Public function say ();
}

Abstract Class A implements work{
Public Function Showlove () {
echo ' Love you<br/> ';
}
}

Class B extends a{
Public function say () {
Echo ' Hello, i m in B ';
}
}

$k =new B ();
$k->say ();

/*
The above procedures can be performed normally
The ordinary class implements interface, it becomes an abstract class, it is like directly to the abstract class added an abstract method!
*/

Second, the combination of interface and inheritance

The parent class is the normal class, after the subclass inherits, implements the interface in the class at the same time!

Question: Does it make sense to have such an application in real-world development?

*/
Interface kk{
Public function say ();
}

Class A {
Public function Show () {
Echo ' I am the parent <br/> ';
}
}

Class B extends a implements kk{
Public function say () {
Echo ' I was inheriting a class, while realizing the say interface of <br/> ';
}

}

$b =new B ();
$b->show ();//I am the parent class

$b->say ();//I am inheriting the class A, while implementing the Say interface

?>

PHP abstraction and interface differences [reprint]

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.