473,326 Members | 2,805 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,326 software developers and data experts.

Check if Object implements interface

bilibytes
128 100+
Hello,

I have a little question.

I would like to know how to check if an object is compliant with an interface.

I know that if the Class implements explicitly the Interface, like this:
Expand|Select|Wrap|Line Numbers
  1. Class RobertNestaMarley implements Smoker_Interface
the operator instanceof will let me know. like this:

Expand|Select|Wrap|Line Numbers
  1. $bobMarley = new RobertNestaMarley()
  2. if ($bobMarley instanceof Smoker_Interface) {
  3.     echo 'Bob likes to smoke';
  4. }
However in my case the Class does not implement the interface explicitly.

so my only goal is to know if $bobmarley has some methods i need from it.

Lets say my interface is like this:
Expand|Select|Wrap|Line Numbers
  1. Interface Smoker_Interface
  2. {
  3.     public function lightACigar($brand);
  4. }
I want to be able to know if $bobMarley has such a method in his class. And so i could call:
Expand|Select|Wrap|Line Numbers
  1. $bobMarley->lightACigar('Cohiba');

Do you know how to do this?

Regards

Bilibytes
Jul 26 '09 #1
11 15501
Markus
6,050 Expert 4TB
So you want to know if an object implements a method, but the object doesn't necessarily implement the interface? I don't quite understand. If the interface exists, you should just use to instanceof to check compatibility.

You might explain a little more. Do you just want to know if a class has a method?
Jul 26 '09 #2
bilibytes
128 100+
Thanks for your reply.
Your solution works well if i just want to check a few methods, but if i have an interface that is quite big, it will not be very efficient.

What I would like is something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Interface Smoker_Interface
  3. {
  4.     public function lightACigar($brand);
  5. }
  6.  
  7. class BobMarley
  8. {
  9.     protected $_cigarInMouth;
  10.  
  11.     public function lightACigar($brand){
  12.            $this->_cigarInMouth    = $brand;
  13.     }
  14.     private function otherFunciton()
  15.     {
  16.     $somethingelse = true;
  17.     }
  18. }
  19.  
  20. $bobMarley = new BobMarley();
  21. if ($bobMarley instanceof Smoker_Interface) {
  22.     echo 'Bob likes to smoke';
  23. }
  24.  
in the code above, the instance of will return false and 'Bob likes to smoke' will not be echoed.

I would like something that would return true if the Class has the methods that are present in the interface even though it doesn't explicitly implement the interface with the word implements
Jul 26 '09 #3
bilibytes
128 100+
I am doing a reusable class that filters the url input. Let's call it Url_Sanitize.
Url_Sanitize uses a filter which can be of any type. The only restriction for that filter object, is to implement one interface, so that the Url_Sanitize can fearlessly (of an exception) call the filter methods that are specified in the interface.

Ok i have the solution, i think...

If the person wants to pass a filter object, the filter class must be extended and implement my interface. That's it!

Applied to my example it would be something like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Interface Smoker_Interface
  3. {
  4.     public function lightACigar($brand);
  5. }
  6.  
  7. class BobMarley
  8. {
  9. protected $_cigarInMouth;
  10.     public function lightACigar($brand){
  11.            $this->_cigarInMouth    = $brand;
  12.     }
  13.     private function otherFunciton()
  14.     {
  15.     $somethingelse = true;
  16.     }
  17. }
  18.  
  19. class BobMarley_SmokerCompliant extends BobMarley implements Smoker_Interface
  20. {
  21. }
  22.  
  23. $bobMarley = new BobMarley();
  24. if ($bobMarley instanceof Smoker_Interface) {
  25.     echo 'Bob likes to smoke';
  26. }
  27.  
You see, when people talk they solve problems!

That's why this forum is good!

Regards

Bilibytes
Jul 26 '09 #4
Dormilich
8,658 Expert Mod 8TB
@bilibytes
I also had this problem once. =)
Jul 26 '09 #5
Markus
6,050 Expert 4TB
Well, if you want to impose a strict API, then you should use using an interface - that's what they're for... no?
Jul 27 '09 #6
bilibytes
128 100+
Yes mark,

i have the interface, however the problem i had was that i didn't know how to check if the user defined class implemented my interface without forcing him to use implements.
I didn't want to force him to use implements because his class may be part of another framework such as Zend_Framework. And it's no good practice to change the code of existing classes (at least if you are not it's developer).
But then i finally came to the fact that the user could subclass the given framework class and force it to implement my interface.
like i did here:
Expand|Select|Wrap|Line Numbers
  1. class UserSubclasses_ZendFilterInput extends Zend_Filter_Input implements Bilibytes_Interface
  2. {
  3. }

Did i make it clear? lol i don't think so, it's quite difficult to explain

Regards
Jul 27 '09 #7
Markus
6,050 Expert 4TB
Lol. If you have an interface, and you have it for the reason that any class that you are going to be interacting with needs to conform to some implementation (that is, your interface), then you should force them to implement your interface. That's the point of an interface: you know what the object provides.

I don't see how having to implement an interface would get in the way of the Zend framework? Can you explain?

I'm not trying to sound rude - I just sound that way a lot of the time :P

Mark <likes a good chat>.
Jul 27 '09 #8
bilibytes
128 100+
Mark, my problem is solved!! we can close this thread!! lol

here is the explanation:

I have created a class that handles the url params.

this class is called Bb_Url_Sanitize

it uses a filter, for the moment i use Zend_Filter_Input, which is good for me.

As i have created the Bb_Url_Sanitize class based on Zend_Filter_Input, and i have used some of the methods of Zend_Filter_Input.

AND i want the users to be able to provide their own FIlter classes, their filter MUST have the methods of Zend_Filter_Input that i used in Bb_Url_Sanitize.

So i created an interface to ensure that the filter has at least the methods i use in Bb_Url_Sanitize. And i have subclassed Zend_Filter_Input:
Expand|Select|Wrap|Line Numbers
  1.  Bb_Url_Sanitize_ZFI extends Zend_Filter_Input implements Bb_Url_Sanitize_Filter_Interface
Therefore in Bb_Url_Sanitize::setFilter($filter) i check that the $filter is an instanceof Bb_Url_Sanitize_Filter_Interface and i'm able to use that filter with no fear of throwing exceptions...

did you understand my problem and solution?

regards

bilibytes
Jul 27 '09 #9
Markus
6,050 Expert 4TB
Yeah, I get you.

One thing - beware of Zend's evil behaviour: long class names are annoying! :P
Jul 27 '09 #10
bilibytes
128 100+
@Markus
I didn't know about that, why? :S
Jul 27 '09 #11
Dormilich
8,658 Expert Mod 8TB
just a further notice: you can implement more than one interface to a class.
Jul 27 '09 #12

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

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: Joe Rattz | last post by:
Ok, I am trying to figure out how to tell if a class (not object) implements a particular interface. Using the "is" operator does not seem to work for me and I assume its because I have a class,...
8
by: Joe Johnston | last post by:
I need a Browser Helper object written in VB.NET Please point me at a good example. Joe MCPx3 ~ Hoping this MSDN ng three day turnaround is true. Additional info: What is a BHO? In its...
4
by: Lucas Tam | last post by:
Hi all, I like to dynamically check if an object implements a particular interface. Currently I am doing: Ctype(Object, IInterface) If the object implements the interface it will not...
1
by: Lucile | last post by:
Hi to All, I am new to this newsgroup, so please bare with me. Here is the deal: I have the following Object Graph: Icell (Interface) Iline (Interface) Itable (Interface) Classes: Cell:...
4
by: pipehappy | last post by:
Hello everyone: Is there a way to check the type when do assignment? if I write: ab = bc and want to make sure the return value of isinstance(bc, klass) is True or I will raise a exception.
2
by: lothar.behrens | last post by:
Hi, I am trying to load an assembly at runtime (plugin). Having the object (as Object), I like to cast it to the interface. The interface is defined as follows: Public Interface...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
16
by: Alex | last post by:
Hello, I'm trying to use a (remote) COM object from a PHP script (4.4, server has apache2 win32). The basics seem to work : I instantiate the COM object ($o = new COM"..."), then I use two of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.