469,352 Members | 2,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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 13988
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,652 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,652 Expert Mod 8TB
just a further notice: you can implement more than one interface to a class.
Jul 27 '09 #12

Post your reply

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

Similar topics

8 posts views Thread by Joe Johnston | last post: by
4 posts views Thread by Lucas Tam | last post: by
4 posts views Thread by pipehappy | last post: by
2 posts views Thread by lothar.behrens | last post: by
16 posts views Thread by Alex | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.