PHP's abstract class, interface difference and choice ____php

Source: Internet
Author: User

difference:

1, the use of the interface is through the keyword implements. The use of abstract classes is extends by keyword. Of course, interfaces can also be inherited by keyword extends.

2. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. You can declare various types of member variables in an abstract class to implement the encapsulation of the data. (The member variables in the other Java interface are declared as public static final types)

<?php 
abstract class Father {
	function meth1 () {
		echo "meth1...<br>";
	}
	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...<br>";
	}
}
$s =new Son ();
Echo $s->var1. " <br> ";
echo Father:: $var 2. " <br> ";
echo Father::var3. " <br> ";


Interface ifather {
	//public $iVar 1= "iVar1";        The interface definition cannot contain the member variable
	//public static $iVar 2= "IVar2", where the static variable
	const ivar3= "IVAR3"
	cannot be included in the interface definition; function iMeth1 ();
}
Class Ison implements Ifather {
	function iMeth1 () {
		echo "imeth1...<br>";
	}
}
$is =new Ison ();
echo ifather::ivar3;
? >


3, the interface does not have constructors, abstract classes can have constructors.

4, the methods in the interface are public types by default, and 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 can only inherit from an abstract class.


an abstract class or an interface. If you want to create a model that will be adopted by tightly related objects, you can use abstract classes. If you want to create a feature that will be taken by some unrelated objects, use an interface. If you must inherit behavior from multiple sources, use an interface. If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.

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.