3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { protected $suit; protected $value; protected $suit_text; protected $value_text; protected $facedown = false; public function __construct($suit, $value) { $suits = array( 'H' => 'Hearts', 'C' => 'Clubs', 'D' => 'Diamonds', 'S' => 'Spades' ); $values = array( 'A' => 'Ace', '2' => 'Two', '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six', '7' => 'Seven', '8' => 'Eight', '9' => 'Nine', '10' => 'Ten', 'J' => 'Jack', 'Q' => 'Queen', 'K' => 'King' ); $this->suit = $suit; $this->value = $value; $this->suit_text = $suits[$suit]; $this->value_text = $values[$value]; } /** * @return string */ public function getSuit() { return $this->suit; } /** * @return string */ public function getValue() { return $this->value; } /** * @return string */ public function getAsText() { return $this->value_text.' of '.$this->suit_text; } /** * @return string */ public function getSuitAsText() { return $this->suit_text; } /** * @return string */ public function getValueAsText() { return $this->value_text; } /** * Generates HTML for the card * @param string $id * @return string */ public function getHtml($id = null) { $html = '<div id="'.$id.'" class="playing-card card-' .strtolower($this->getValue()) .strtolower($this->getSuit()).' '; if($this->facedown == true){$html .= 'playing-card-facedown ';} $html .= '"></div>'; return $html; } /** * @return string */ public function getJson() { $class = 'playing-card card-'.strtolower($this->getValue()).strtolower($this->getSuit()).' '; if($this->facedown == true){$class .= 'playing-card-facedown ';} $array = array( 'suit' => $this->getSuit(), 'value' => $this->getValue(), 'suit_text' => $this->getSuitAsText(), 'value_text' => $this->getValueAsText(), 'facedown' => $this->facedown, 'divclass' => $class ); return json_encode($array); } /** * Flips the card face up or face down */ public function flipCard() { if($this->facedown == false) { $this->facedown = true; } else { $this->facedown = false; } } /** * Flips the card face down */ public function flipFaceDown() { $this->facedown = true; } /** * Flips the card face up */ public function flipFaceUp() { $this->facedown = false; } /** * @return bool */ public function isFaceDown() { if($this->facedown == true) { return true; } return false; } } $cards = []; $hand = 'As Ks Qs Js 10s'; $array = explode(' ', $hand); foreach ($array as $card) { $val = substr($card, 0, -1); $suit = strtoupper(substr($card, -1)); $cards[] = new Card($suit, $val); } var_dump($cards);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(5) { [0]=> object(Card)#1 (5) { ["suit":protected]=> string(1) "S" ["value":protected]=> string(1) "A" ["suit_text":protected]=> string(6) "Spades" ["value_text":protected]=> string(3) "Ace" ["facedown":protected]=> bool(false) } [1]=> object(Card)#2 (5) { ["suit":protected]=> string(1) "S" ["value":protected]=> string(1) "K" ["suit_text":protected]=> string(6) "Spades" ["value_text":protected]=> string(4) "King" ["facedown":protected]=> bool(false) } [2]=> object(Card)#3 (5) { ["suit":protected]=> string(1) "S" ["value":protected]=> string(1) "Q" ["suit_text":protected]=> string(6) "Spades" ["value_text":protected]=> string(5) "Queen" ["facedown":protected]=> bool(false) } [3]=> object(Card)#4 (5) { ["suit":protected]=> string(1) "S" ["value":protected]=> string(1) "J" ["suit_text":protected]=> string(6) "Spades" ["value_text":protected]=> string(4) "Jack" ["facedown":protected]=> bool(false) } [4]=> object(Card)#5 (5) { ["suit":protected]=> string(1) "S" ["value":protected]=> string(2) "10" ["suit_text":protected]=> string(6) "Spades" ["value_text":protected]=> string(3) "Ten" ["facedown":protected]=> bool(false) } }

preferences:
181.28 ms | 406 KiB | 211 Q