PHP abstract class, interface differences and selection analysis, PHP abstraction
This paper analyzes the differences and choices of abstract class and interface in PHP. Share to everyone for your reference, as follows:
Difference:
1, the use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course the interface can also be inherited by the keyword extends.
2. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. Abstract classes can declare various types of member variables to implement the encapsulation of data. (The member variables in the Java interface are declared as public static final type)
<?phpabstract class Father { function meth1 () { echo "meth1 ...
"; } Abstract function meth2 (); Public $var 1= "var1"; public static $var 2= "Var2"; Const var3= "VAR3";} Class Son extends Father { function meth2 () { echo "meth2 of son ...
"; }} $s =new Son (); Echo $s->var1. "
"; Echo Father:: $var 2."
"; Echo Father::var3."
"; Interface ifather { //public $iVar 1=" IVar1 ", where the interface definition cannot contain member variables //public static $iVar 2=" IVar2 "; The static variable const ivar3= "IVAR3" cannot be included in the interface definition here; function iMeth1 ();} Class ISon implements Ifather { function iMeth1 () { echo "iMeth1 ...
"; }} $is =new ISon (); Echo ifather::ivar3;? >
3, interfaces do not have constructors, abstract classes can have constructors.
4, the method in the interface is the public type by default, and the methods in the abstract class can be decorated with private,protected,public.
5. A class can implement multiple interfaces at the same time, but a class may inherit only one abstract class.
Abstract class or interface
1. If you want to create a model, this model will be used by some closely related objects, you can use abstract classes. The interface is used if you want to create features that will be used by some unrelated objects.
2. If the behavior must be inherited from multiple sources, the interface is used.
3. If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.
More interested in PHP related content readers can view this site: "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL Database Operations Primer, and PHP Common database operations Tips Summary
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- PHP Object-oriented programming (OOP) learning notes (i)-abstract classes, object interfaces, instanceof, and contract programming
- Introduction to PHP opp mechanisms and patterns (abstract classes, interfaces, and contractual programming)
- PHP interfaces and abstract classes using examples
- An analysis of the concepts and differences between abstract classes and interfaces in PHP
- In-depth analysis of the differences between interfaces and abstract classes in PHP
- Introduction to "interface-oriented programming" and "coupling method" with interface, abstract class and common base class in PHP
- The actual function of PHP interface class and abstract class
http://www.bkjia.com/PHPjc/1117084.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117084.html techarticle PHP abstract class, interface differences and selection analysis, PHP abstract This article analyzes the PHP abstract class, interface differences and choices. Share to everyone for your reference, as follows: Difference ...