
Originally Posted by
abalfazl
May you tell me advantages of using interface instead of abstract class?
Because PHP does not support multiple inheritance:
PHP Code:
abstract class AbstractBaseOne {
abstract function do_this();
}
abstract class AbstractBaseTwo {
abstract function do_that();
}
class Concrete extends <no way to extend AbstractBaseOne and AbstractBaseTwo > {
function do_this() {
echo "this";
}
function do_that() {
echo "that";
}
}
But this does work:
PHP Code:
interface AbstractBaseOne {
function do_this();
}
interface AbstractBaseTwo {
function do_that();
}
class Concrete implements AbstractBaseOne, AbstractBaseTwo {
function do_this() {
echo "this";
}
function do_that() {
echo "that";
}
}
What are advantages of using interface instead of Multiple inheritance?
Beyond the conceptual simplicity of interfaces, I can't think of any advantages.
Multiple inheritance lets you include functionality in otherwise hetrogenious classes. Mixins (mixing functionality into a class) and duck typing (if it walks like a duck, and walks like a duck.. it might as well be a Duck) provide the advantages of multiple inheritance without the conceptual overhead.
PHP supports duck typing for user objects, but does not support mixins. I believe there are people working on mixin support for PHP.
Douglas
Bookmarks