Page 1 of 1

[SOLVED] Weird problem with abstract classes and interfaces

Posted: Sat Jan 01, 2005 9:43 am
by Lars79
Hello,

I have an abstract class implementing an interface. This abstract class doesn't contain all of the methods required by the interface. I have a second class that inherits from the first one and contains the missing methods. The code produces no error, but doesn't work.

Simple examle:

Code: Select all

<?php

interface MyInterface {
	
	public function printTest();
	
}

abstract class A
implements MyInterface {
	
	protected $testString = "Hello world";
	
}

class B
extends A{
	
	public function printTest() {
		echo($testString);
	}
	
}

$obj = new B();
$obj->printTest();

?>
This way of coding is no problem in Java, but doesn't seem to work in PHP5. Any explanation or workaround is beeing appreciated. Everything tested on Linux with PHP 5.0.3 on Apache 1.3.33.

Lars


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Jan 01, 2005 9:59 am
by timvw
that is because you are mistaken

change B a bit so it looks like

Code: Select all

class B
extends A{

    public function printTest() {
        echo($this->testString);
    }

}
and it works.... :)

Posted: Sat Jan 01, 2005 10:05 am
by Lars79
Oops, guess I had too much drinks yesterday =).

Thanks and happy new year.