Page 1 of 1

Interfaces & Abstract classes

Posted: Sat Apr 16, 2011 10:48 am
by Tal Aviel
Hey,
I want to have an interface, let's name it as IPage.
Then I want to have an abstract class which implements IPage, and have for some of the functions in IPage implimetations and some are abstract.
Then I have a class which extends the abstract class (which implments IPage) and this class should contain the implimetations of abstract functions in the abstract class which it extends + some override for "default" functions I have in the abstract class.

I tried it:
IPage.php:

Code: Select all

<?php
	interface IPage {
		public function ShowPage($pageProperties);
		public function GetHeaderInformation();
		public function SetDb($db);
	}
?>
Page.php:

Code: Select all

<?php
	abstract class Page implements IPage
	 {
		var $_db;
		public function SetDb($db) {
			$this->_db = $db;
		}
		public function GetHeaderInformation() {
			$pageInformation = new PageInformation();
			return $pageInformation();
		}
		public abstract function ShowPage();
	 }  
?>
about.php:

Code: Select all

<?php
	class SpeceficPage extends Page
	 {
		public function ShowPage()
		 {
			echo "Page Content";
		 }
	 }
?>
index.php:

Code: Select all

<?php
	require "interfaces/IPage.php";
	require "abstract/Page.php";
	require "pages/about.php";
	
	$aboutPage = new SpeceficPage();
	$aboutPage->ShowPage();
?>
And get the following error:

Fatal error: Can't inherit abstract function IPage::ShowPage() (previously declared abstract in Page) in /home/virtual/talaviel/public_html/tries/abstract/Page.php on line 2

Someone can help me??

Thank you !

Tal

Re: Interfaces & Abstract classes

Posted: Sat Apr 16, 2011 11:46 am
by Weirdan
Drop declaration of ShowPage from the abstract class. Descendant classes would still be required to implement it, because it's mandated by inherited interface.

Re: Interfaces & Abstract classes

Posted: Sat Apr 16, 2011 12:13 pm
by Tal Aviel
Thank you!