[SOLVED] Weird problem with abstract classes and interfaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lars79
Forum Newbie
Posts: 12
Joined: Sat Nov 27, 2004 1:57 pm

[SOLVED] Weird problem with abstract classes and interfaces

Post 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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.... :)
Lars79
Forum Newbie
Posts: 12
Joined: Sat Nov 27, 2004 1:57 pm

Post by Lars79 »

Oops, guess I had too much drinks yesterday =).

Thanks and happy new year.
Post Reply