PHP 5 Abstract Classes and Interfaces

oop php

Fri Jan 30 13:11:00 -0500 2009

I wanted to create an interface that would be implemented by a group of classes all extending the same abstract class. Curiosity led me to try implementing the interface in the abstract class alone, forcing the interface’s requirements down to descendants by way of abstract functions fulfilling the interface’s requirements and in turn obligating descendant classes to implement them.

That’s probably a bad idea for a number of reasons (including rendering the interface declaration redundant) but I wanted to see what would happen….

$ php -v
PHP 5.2.4-2ubuntu5.4 with Suhosin-Patch 0.9.6.2 (cli) (built: Nov 25 2008 22:10:31) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

$ php -a
Interactive shell

php > interface An_Interface {
php { public function search();
php { }
php >php > abstract class The_Abstract_Class implements An_Interface {
php { abstract public function search();
php { }

Fatal error: Can't inherit abstract function An_Interface::search() (previously declared abstract in The_Abstract_Class) in php shell code on line 1
blog comments powered by Disqus