How can one describe a rock-paper-scissors relationship between 3 items?
        Posted  
        
            by 
                Madara Uchiha
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Madara Uchiha
        
        
        
        Published on 2012-10-03T21:25:16Z
        Indexed on 
            2012/10/03
            21:37 UTC
        
        
        Read the original article
        Hit count: 283
        
Let's say I have the following structure:
abstract class Hand {}
class Rock extends Hand {}
class Paper extends Hand {}
class Scissors extends Hand {}
The goal is to make a function (or a method) Hand::compareHands(Hand $hand1, Hand $hand2), which would return the winning hand in a rock-paper-scissors match.
That would be very easy with a bunch of ifs, but the point is to have a more robust structure, that's relying on polymorphism rather than on procedural code.
P.S. this is done in actual production code, if someone is asking. This isn't some sort of challenge or homework. (It's not really rock-paper-scissors, but you get the point).
© Stack Overflow or respective owner