Check if Object implements interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bilibytes
    New Member
    • Jun 2008
    • 128

    Check if Object implements interface

    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:
    Code:
    Class RobertNestaMarley implements Smoker_Interface
    the operator instanceof will let me know. like this:

    Code:
    $bobMarley = new RobertNestaMarley()
    if ($bobMarley instanceof Smoker_Interface) {
        echo 'Bob likes to smoke';
    }
    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:
    Code:
    Interface Smoker_Interface
    {
        public function lightACigar($brand);
    }
    I want to be able to know if $bobMarley has such a method in his class. And so i could call:
    Code:
    $bobMarley->lightACigar('Cohiba');

    Do you know how to do this?

    Regards

    Bilibytes
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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?

    Comment

    • bilibytes
      New Member
      • Jun 2008
      • 128

      #3
      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:
      Code:
      <?php
      Interface Smoker_Interface
      {
          public function lightACigar($brand);
      }
      
      class BobMarley
      {
          protected $_cigarInMouth;
      
          public function lightACigar($brand){
                 $this->_cigarInMouth	= $brand;
          }
          private function otherFunciton()
          {
      	$somethingelse = true;
          }
      }
      
      $bobMarley = new BobMarley();
      if ($bobMarley instanceof Smoker_Interface) {
          echo 'Bob likes to smoke';
      }
      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

      Comment

      • bilibytes
        New Member
        • Jun 2008
        • 128

        #4
        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:

        Code:
        <?php
        Interface Smoker_Interface
        {
            public function lightACigar($brand);
        }
        
        class BobMarley
        {
        protected $_cigarInMouth;
            public function lightACigar($brand){
                   $this->_cigarInMouth	= $brand;
            }
            private function otherFunciton()
            {
        	$somethingelse = true;
            }
        }
        
        class BobMarley_SmokerCompliant [B]extends[/B] [I]BobMarley[/I] [B]implements[/B] [I]Smoker_Interface[/I]
        {
        }
        
        $bobMarley = new BobMarley();
        if ($bobMarley instanceof Smoker_Interface) {
            echo 'Bob likes to smoke';
        }
        You see, when people talk they solve problems!

        That's why this forum is good!

        Regards

        Bilibytes

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by bilibytes
          If the person wants to pass a filter object, the filter class must be extended and implement my interface.
          I also had this problem once. =)

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Well, if you want to impose a strict API, then you should use using an interface - that's what they're for... no?

            Comment

            • bilibytes
              New Member
              • Jun 2008
              • 128

              #7
              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:
              Code:
              class UserSubclasses_ZendFilterInput extends Zend_Filter_Input implements Bilibytes_Interface
              {
              }

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

              Regards

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                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>.

                Comment

                • bilibytes
                  New Member
                  • Jun 2008
                  • 128

                  #9
                  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:
                  Code:
                   Bb_Url_Sanitize_ZFI [B]extends[/B] Zend_Filter_Input[B] implements[/B] 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

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Yeah, I get you.

                    One thing - beware of Zend's evil behaviour: long class names are annoying! :P

                    Comment

                    • bilibytes
                      New Member
                      • Jun 2008
                      • 128

                      #11
                      Originally posted by Markus
                      Yeah, I get you.

                      One thing - beware of Zend's evil behaviour: long class names are annoying! :P
                      I didn't know about that, why? :S

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        just a further notice: you can implement more than one interface to a class.

                        Comment

                        Working...