473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Overriding an interface method

GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. interface IOutput
  2. {
  3.   string Output();
  4. }
  5.  
  6. class BaseObject : IOutput
  7. {
  8.   public virtual string Output()
  9.   {
  10.     return "BaseObject's Output";
  11.   }
  12. }
  13.  
  14. class ComplexObject : BaseObject
  15. {
  16.   public override string Output()
  17.   {
  18.     return "ComplexObject's Output";
  19.   }
  20. }
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!
May 16 '11 #1

✓ answered by Dormilich

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
Expand|Select|Wrap|Line Numbers
  1. interface IOutput
  2. {
  3.     public function output();
  4. }
  5.  
  6. class Test1 implements IOutput
  7. {
  8.     public function output()
  9.     {
  10.         return "test1";
  11.     }
  12. }
  13.  
  14. class Test2 extends Test1
  15. {
  16.     public function output()
  17.     {
  18.         return "test2";
  19.     }
  20. }
  21.  
  22. $ex = new Test2;
  23. echo $ex->output(), PHP_EOL; // test2
  24. // test if Test2 adheres to IOutput
  25. var_dump(($ex instanceof IOutput)); // bool(true)

6 5064
Dormilich
8,658 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. interface IOutput
  2. {
  3.     public function output();
  4. }
  5.  
  6. class Test1 implements IOutput
  7. {
  8.     public function output()
  9.     {
  10.         return "test1";
  11.     }
  12. }
  13.  
  14. class Test2 extends Test1
  15. {
  16.     public function output()
  17.     {
  18.         return "test2";
  19.     }
  20. }
  21.  
  22. $ex = new Test2;
  23. echo $ex->output(), PHP_EOL; // test2
  24. // test if Test2 adheres to IOutput
  25. var_dump(($ex instanceof IOutput)); // bool(true)
May 17 '11 #2
GaryTexmo
1,501 Expert 1GB
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!
May 17 '11 #3
Dormilich
8,658 Expert Mod 8TB
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.
May 17 '11 #4
GaryTexmo
1,501 Expert 1GB
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 :D I'm glad we can get rid of that now.)
May 17 '11 #5
Dormilich
8,658 Expert Mod 8TB
I also don't know what version of PHP we're using.
make a .php page that contains the following:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. phpinfo();
and call that. it will list all interesting and necessary settings.

another great section of PHP: SPL (the Standard PHP Library)
May 17 '11 #6
GaryTexmo
1,501 Expert 1GB
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! :)
May 17 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: daechulsohn | last post by:
Just thought I would contribute some knowledge for a change. After hours tracking down a problem with obfuscated code, where we were getting the exception : System.TypeLoadException: Method a...
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
2
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually...
8
by: archana | last post by:
Hi all, I have one base class and 2 derived class and one method say xyz. in base class i declare xyz as virtual, but i want to override to override this method in derived2 class and not in...
0
by: ddddd | last post by:
AM using the JDBC interface metho getProcedures() to get the stored procedures from the Database... Since this is common interface method am trying out with two different set of databases namely...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
1
by: dba | last post by:
Hello All, And thanks in advance for taking the time to read this. I was given an interface definition to a COM object. Now I'd like to be able to compile the interface into it's own assembly...
2
by: Builder | last post by:
Hi, I imported following COM interface DECLARE_INTERFACE(IAxMediaCB) { STDMETHOD(BufferCB) ( LONGLONG theStartTime,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.