You'll have to forgive me here, I'm a C# developer so my PHP lingo might not be quite right. I'll try to explain the problem as best I can and hopefully you guys get the idea.
I want to define a method in an interface, then implement that interface in what will be a base class. Then I want to override that method in any class that inherits from my base class. If I were to do this in C#, here's a rough example what I'd do...
Expand|Select|Wrap|Line Numbers
- interface IOutput
- {
- string Output();
- }
- class BaseObject : IOutput
- {
- public virtual string Output()
- {
- return "BaseObject's Output";
- }
- }
- class ComplexObject : BaseObject
- {
- public override string Output()
- {
- return "ComplexObject's Output";
- }
- }
Now I did some digging to try to find a solution and couldn't find anything via google, per se. I'm betting it's just a semantics difference between C# and PHP that's got me confused here, but I was able to find out that in PHP, an interface can inherit from another class. This means that, in my above example, I can define a class that defines my Output method and have my interface extend it. Then I can have my base class implement the interface and have my other class extend the first one, which would then let me override the Output method in both those classes.
This seems a bit confusing so I wanted to check in with people who are more familiar with PHP than I am. Is this the correct way to go about it? If not, what is a better way?
The overall goal here is to define an interface that will ensure my classes have a certain method, but I want that method to do different things for the classes in my inheritance structure. Any help would be greatly appreciated.
Thanks!