Overriding an interface method – PHP

  php

Q(Question):

Hey all,

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…

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";
}
}

When I try to do similar in PHP, I get an error. I don’t have the error handy at the moment, but iirc it was something along the lines of it wouldn’t let me re-declare the method defined in the interface.

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!

A(Answer):

in PHP interfaces only can inherit from other interfaces and classes can only implement interfaces. Once a class has implemented an interface, all derived (child) classes automatically implement that interface.

example

interface IOutput
{
public function output();
}
class Test1 implements IOutput
{
public function output()
{
return "test1";
}
}
class Test2 extends Test1
{
public function output()
{
return "test2";
}
}
$ex = new Test2;
echo $ex->output(), PHP_EOL; // test2
// test if Test2 adheres to IOutput
var_dump(($ex instanceof IOutput)); // bool(true)

A(Answer):

That worked for you? I tried that and got a compiler error.. the exact same code. It said that output was already declared (or something). I’ll try again and let you know.

You’ve also inadvertently given me the answer to something else that’s been plaguing me… I couldn’t find a php equivalent of "is" to test types in php. Apparently it’s "instanceof" which is absolutely fantastic.

Thank you!

A(Answer):

That worked for you? I tried that and got a compiler error.. the exact same code.

what compiler? PHP only has a parser. that code won’t work in PHP 4, though. post the error message, I probably can make sense of it. (and I did omit something … the opening <?php tag)

I couldn’t find a php equivalent of "is" to test types in php.

see also Type Hinting.

A(Answer):

Sorry, yes the parser… when it runs. I’m not used to web programming so I use the lingo of what I’m more familiar with 😉

I don’t have the error, I was trying to repeat it from what I saw when I was working on this on the weekend. I’m going to try it again and get back to you. I also don’t know what version of PHP we’re using. I’m assisting a friend with a project he’s working on, so I’m kind of learning PHP as I go.

But yea, I’ll give this a try and get back to you with how it went.

(We did find the Type Hinting, which is great since that’s how I’m used to passing arguments to a method, we just couldn’t figure out how to test objects in a list to see what type they were. So of course, being programmers, we came up with a hack to workaround it 😀 I’m glad we can get rid of that now.)

A(Answer):

I also don’t know what version of PHP we’re using.

make a .php page that contains the following:

<?php
phpinfo();

and call that. it will list all interesting and necessary settings.

another great section of PHP: SPL (the Standard PHP Library)

A(Answer):

I had my friend check it out and the solution you proposed works. Which is exactly how I would have expected it to work, so I must have made some other mistake when I was experimenting with it on the weekend.

Thanks for your help! 🙂

LEAVE A COMMENT