SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Duck Typing,Polymorphism

    Hello!

    I want to know when using of duck typing is better than using of inheritance or interface in order to polymorphism?

    In fact,I want to know what is the advantage of using duck typing in polymorphism?


    Please check this:


    Thanks in advance
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abalfazl View Post
    I want to know when using of duck typing is better than using of inheritance or interface in order to polymorphism?
    Always. Interfaces have two uses; To allow duck typing in static languages, and to carry meta information about a class. The former is irrelevant in PHP.

  3. #3
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello!



    Always. Interfaces have two uses; To allow duck typing in static languages, and to carry meta information about a class. The former is irrelevant in PHP.
    May you explain that by example?



    May you give me example about using ducktyping in PHP for polymorphism?
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  4. #4
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abalfazl View Post
    Hello!
    May you give me example about using ducktyping in PHP for polymorphism?
    Ducktyping is could also be called implicit polymorphism because classes don't have to have a relationship but can still be access in a polymorphic way.
    Quote Originally Posted by abalfazl View Post
    May you explain that by example?
    You can use empty interfaces to add extra data to the class so you can derive certain data from that.
    Last edited by Jasper Bekkers; Jul 27, 2007 at 08:29.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  5. #5
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Jasber!

    May you show example of duck typing by code?

    Thanks
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  6. #6
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abalfazl View Post
    Hello Jasber!

    May you show example of duck typing by code?

    Thanks
    One place duck-typing is extremely useful is when building dynamic proxies because they can't adhere to an interface.

    PHP Code:
    <?php
    class Auth{
        public function 
    can_i($object$method){
            
    // deny access to class foo
            // NOTE: this is for demonstration purposes, and has nothing to do with duck-typing
            
    if($object instanceof Foo) return false;
            return 
    true;
        }
    }

    class 
    Auth_Proxy{
        private 
    $object;
        private 
    $auth;
        
        public function 
    __construct(Auth $auth$object){
            
    $this->auth $auth;
            
    $this->object $object;
        }
        
        public function 
    __call($method$arguments){
            if(
    $this->auth->can_i($this->object$method)){
                
    call_user_func_array(array($this->object$method), $arguments);
            }else{
                throw new 
    Exception('Not allowed to execute this method!');
            }
        }
    }

    class 
    Foo{
        public function 
    bar(){
            echo 
    'Foo::bar';
        }
    }

    class 
    Bar{
        public function 
    foo($obj){
            
    $obj->bar();
        }
    }

    class 
    Bar2{
        public function 
    foo(Foo $obj){
            
    $obj->bar();
        }
    }
    When using this like like this it works perfectly:
    PHP Code:
    $bar = new Bar();
    $auth = new Auth();
    $foo = new Auth_Proxy($auth, new Foo());

    // here we can use the class no matter what because of the duck-typing
    $bar->foo($foo); 
    However if we'd introduce type-hinting into our function like we do in Bar2::foo this code will never be able to execute.
    PHP Code:
    $bar2 = new Bar2();
    $auth = new Auth();
    $foo = new Auth_Proxy($auth, new Foo());

    // because of the type hinting Bar2 prohibits us from using duck-typing, thus this will never work
    $bar2->foo($foo); 
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An interesting blog entry on why interfaces are useful,

    http://benpryor.com/blog/index.php?/...nterfaces.html

  8. #8
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can anybody give me some snippet for smtp-based email validation using PHP?

    http://vic2x-programming.blogspot.com/

  9. #9
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    An interesting blog entry on why interfaces are useful,

    http://benpryor.com/blog/index.php?/...nterfaces.html
    Yeah, in Java. Interfaces were practically invented in Java in order to circumvent the language's statical typing.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possible although some would argue that Interfaces in Java were there to prevent the MI problem faced in other languages at the time. That argument still holds true for me; Look at other languages that have Interfaces that are not statically typed.

    Could you debunk Interfaces in those languages as well? You could pretty much therefore debunk all those languages that make use of Interfaces in that case.

    I just don't get why it makes any difference if a language is statically or dynamically typed, etc to make any difference one way or another in this argument.

    > Yeah, in Java.

    That expression suggests to me you have a problem with Java it's self... How many years have you developed with this language?

  11. #11
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    > Yeah, in Java.

    That expression suggests to me you have a problem with Java it's self...
    In this precise instance, I wasn't expressing a problem with Java, I was just pointing that the article talks about Java while being used as an example for PHP, while the two languages are quite different in many aspects. But you are correct that I have a problem sith Java itself.

    Quote Originally Posted by Dr Livingston View Post
    How many years have you developed with this language?
    A couple. But in my case that is like about a decade for most other programmers.

  12. #12
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac View Post
    Yeah, in Java. Interfaces were practically invented in Java in order to circumvent the language's statical typing.
    Generics are invented for that purpose. Interface seem more like the equivalent of C++ interfaces. They too separate interface from implementation (most of the time, templates don't).

    Quote Originally Posted by Dr Livingston View Post
    Possible although some would argue that Interfaces in Java were there to prevent the MI problem faced in other languages at the time. That argument still holds true for me; Look at other languages that have Interfaces that are not statically typed.
    Multipele interfaces are almost as bad as multiple inheritance because it will usually violate the separations of concerns. The advantage multiple interfaces have is that they don't suffer from the diamond problem.

    How many years have you developed with this language?
    Years are not a good measurement for experience. One could code for 15 years and still produce rubbish.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the diamond problem that I was inferring about, in that with Java you don't have than problem because of interfaces, rather than for any other reason.

    > One could code for 15 years and still produce rubbish.

    Not in a professional capacity you wouldn't. At least, I can't think of any business or company that would employ you in any position if you were incompetent, or negligent for that matter for any length of time.

  14. #14
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    > One could code for 15 years and still produce rubbish.

    Not in a professional capacity you wouldn't. At least, I can't think of any business or company that would employ you in any position if you were incompetent, or negligent for that matter for any length of time.
    After following http://www.thedailywtf.com for a while I'm not buying that . The IT industry for that matter is a very bad one, there are no mandatory certificates you have to have before you can get a job, there is no union (a least not in my country), and the standard for education is low. The industry is still far from mature and is in need for something similar to the RCPSG that exists for surgeons.
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  15. #15
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    > One could code for 15 years and still produce rubbish.

    Not in a professional capacity you wouldn't. At least, I can't think of any business or company that would employ you in any position if you were incompetent, or negligent for that matter for any length of time.
    It depends on what you mean by rubbish. As long as the code works, I think you're pretty safe even if the quality of the code wouldn't live up to our standards.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •