473,732 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if Object implements interface

bilibytes
128 New Member
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 15642
Markus
6,050 Recognized Expert Expert
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 New Member
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 New Member
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 Recognized Expert Moderator Expert
@bilibytes
I also had this problem once. =)
Jul 26 '09 #5
Markus
6,050 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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_Inp ut, which is good for me.

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

AND i want the users to be able to provide their own FIlter classes, their filter MUST have the methods of Zend_Filter_Inp ut 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_Inp ut:
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($fi lter) i check that the $filter is an instanceof Bb_Url_Sanitize _Filter_Interfa ce 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 Recognized Expert Expert
Yeah, I get you.

One thing - beware of Zend's evil behaviour: long class names are annoying! :P
Jul 27 '09 #10

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

Similar topics

16
25417
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
5
17027
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, not an object I am wanting to check. Here's my scenario. I am dynamically loading assemblies (dlls) at runtime. I am getting the list of types from each assembly, and if that type implements a particular interface, then and only then do I want...
8
5730
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 simplest form, a BHO is a COM in-
4
12392
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 throw an error... Is there a better way I can do this via System.Reflection?
1
1670
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: implements Icell Line: inherits Arraylist Implements Iline
4
3151
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
3415
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 IVoimDescriptor Function getVendorName() As String Function getVoimDescription() As String '...
5
1587
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
2107
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 the objects methods to authenticate. In the meantime a property called "ProjectConnected", passes from 0 to 1. So I think the object is correctly instanciated and the connection works.
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.