Skip to content

Instantly share code, notes, and snippets.

@sonus
Created April 17, 2024 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonus/62db6a40b41f0fd6b3445b7c1e426f8b to your computer and use it in GitHub Desktop.
Save sonus/62db6a40b41f0fd6b3445b7c1e426f8b to your computer and use it in GitHub Desktop.
Depend on Abstractions, not Concretions (Framework)
<?php
abstract class Player {
public abstract function play();
}
class Human extends Player {
public function play() {
echo "Human player makes a move.\n";
}
}
class Computer extends Player {
public function play() {
echo "Computer player makes a move.\n";
}
}
class Round {
private Player $player1;
private Player $player2;
public function __construct(Player $player1, Player $player2) {
$this->player1 = $player1;
$this->player2 = $player2;
}
public function playRound() {
$this->player1->play();
$this->player2->play();
echo "Determine winner or tie based on game rules.\n";
}
}
$humanPlayer = new Human();
$computerPlayer = new Computer();
$round = new Round($humanPlayer, $computerPlayer);
$round->playRound();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment