1. Interface supports multiple inheritance
class Class_A { }
class Class_B { }
abstract class MyAbstractClass extends Class_A, Class_B { }
Parse error: syntax error, unexpected ',', expecting '{' in...
.
2. Interface contains only incomplete member (contains only signature of a method)
interface Animal {
function run() {
echo 'I am running!';
}
}
Fatal error: Interface function Animal::run() cannot contain body in ...
.
3. Interface does not contain data member
interface MyInterface {
public $foo = null;
}
Fatal error: Interfaces may not include member variables in ...
.
4. Interface can not have access modifiers (other than
public
), by default everything is assumed as public
interface iTemplate
{
private function setVariable($name, $var);
public function getHtml($template);
}
Fatal error: Access type for interface method iTemplate::setVariable() must be omitted in ...
.
Notes (mistakes on the page):
1. Interface does contain CONSTRUCTOR (but can force child classes to implement constructor) as well as abstract class can contain one
interface MyInterface {
function __construct();
}
class mathh implements MyInterface {
// function __construct(){ }
}
2. We can have static methods inside interface as well as inside abstract classes
interface MyInterface {
static function getPI();
}
class mathh implements MyInterface {
static function getPI(){
return 3.1415926535;
}
}
3. Abstract class can contain abstract static member
abstract class Animal {
// child classes must implement this
abstract static function prey();
static public function run() {
echo "I am running!\n";
}
}
class Dog extends Animal {
static public function prey() {
echo "I killed the cat !\n";
}
}
$dog = new Dog();
$dog->prey(); // I killed the cat !