$this->getWeapon()->useWeapon();
    }
}
class Knight extends Character
{
    public function fight()
    {
        print "Knight Fighting!!" . PHP_EOL;
        $this->getWeapon()->useWeapon();
    }
}
class Troll extends Character
{
    public function fight()
    {
        print "Troll Fighting!!" . PHP_EOL;
        $this->getWeapon()->useWeapon();
    }
}
//Create a troll
$troll = new Troll();
//Fight using a knife
$troll->setWeapon(new KnifeBehavior())->fight();
//Fight using a Axe
$troll->setWeapon(new AxeBehavior())->fight();
//Create a king
$king = new King();
//Fight using a sword
$king->setWeapon(new SwordBehavior())->fight();
//Fight using a bow and arrow
$king->setWeapon(new BowAndArrowBehavior())->fight();
Example #2
0
 private static function init()
 {
     $king = new King();
     $king->init();
     self::$king = $king;
 }
Example #3
0
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>
      <?php 
class King
{
    public static function proclaim()
    {
        echo "A kingly proclamation!";
    }
}
King::proclaim();
?>
    </p>
  </body>
</html>
Example #4
0
 public static function genList($original, $recurse)
 {
     $board = new ChessBoard();
     $board->loadString($original);
     $outlist = array();
     //echo var_dump(debug_backtrace()) . "\n";
     for ($i = 1; $i < 9; $i++) {
         for ($j = 1; $j < 9; $j++) {
             if ($board->whiteTurn()) {
                 if ($board->get($i, $j) == ChessBoard::$WHITEPAWN) {
                     $piece = new Pawn($i, $j, $board, true);
                 } else {
                     if ($board->get($i, $j) == ChessBoard::$WHITEKING) {
                         $piece = new King($i, $j, $board, true);
                         $piece->setRecurse(recurse);
                     } else {
                         if ($board->get($i, $j) == ChessBoard::$WHITEKNIGHT) {
                             $piece = new Knight($i, $j, $board, true);
                         } else {
                             if ($board->get($i, $j) == ChessBoard::$WHITEROOK) {
                                 $piece = new Rook($i, $j, $board, true);
                             } else {
                                 if ($board->get($i, $j) == ChessBoard::$WHITEBISHOP) {
                                     $piece = new Bishop($i, $j, $board, true);
                                 } else {
                                     if ($board->get($i, $j) == ChessBoard::$WHITEQUEEN) {
                                         $piece = new Queen($i, $j, $board, true);
                                     } else {
                                         $piece = 0;
                                     }
                                 }
                             }
                         }
                     }
                 }
             } else {
                 if ($board->get($i, $j) == ChessBoard::$BLACKPAWN) {
                     $piece = new Pawn($i, $j, $board, false);
                 } else {
                     if ($board->get($i, $j) == ChessBoard::$BLACKKING) {
                         $piece = new King($i, $j, $board, false);
                         $piece->setRecurse(recurse);
                     } else {
                         if ($board->get($i, $j) == ChessBoard::$BLACKKNIGHT) {
                             $piece = new Knight($i, $j, $board, false);
                         } else {
                             if ($board->get($i, $j) == ChessBoard::$BLACKROOK) {
                                 $piece = new Rook($i, $j, $board, false);
                             } else {
                                 if ($board->get($i, $j) == ChessBoard::$BLACKBISHOP) {
                                     $piece = new Bishop($i, $j, $board, false);
                                 } else {
                                     if ($board->get($i, $j) == ChessBoard::$BLACKQUEEN) {
                                         $piece = new Queen($i, $j, $board, false);
                                     } else {
                                         $piece = 0;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($piece != 0) {
                 $list = $piece->generatePreList();
                 foreach ($list as $row) {
                     $temp = $piece->typeString();
                     $temp .= self::toAlg(substr($row, 68, 2)) . ",";
                     $temp .= self::toAlg(substr($row, 70, 2)) . ",";
                     $temp .= $row;
                     $outlist[] = $temp;
                 }
                 $piece = 0;
             }
         }
     }
     return $outlist;
 }