我有一套卡片的單例類,應該在構造函數中創建52張卡片。它看起來像這樣:按值向數組添加對象
protected function __construct() {
global $instance;
$suits = array("clubs", "spades", "hearts", "diamonds");
settype($instance->deck, "array");
foreach ($suits as $suit) {
for ($i = 1; $i <= 13; $i++) {
$card = new Card($suit, $i);
$instance->deck[] = clone $card;
}
}
}
我所得到的是所有充滿了鑽石王的物體。克隆關鍵字不應該阻止這個嗎?對不起,如果這是比我能找到更基本的,我很新的PHP。
編輯:這是Card,Deck和我正在測試的文件的完整定義。不是最好的或最優化的代碼,而是快速的學校作業。我還曾試圖分配$實例 - >甲板[]直接到新卡的對象,但隨後試圖克隆關鍵字後
Card.php
<?php
class Card {
var $suit;
var $cardVal;
var $imageName;
function __construct($suitName, $val) {
global $suit, $cardVal;
$suitName = strtolower($suitName);
if (gettype($val) === "string") {
$val = strtolower($val);
}
switch ($suitName) {
case 'hearts':
$suit = 'Hearts';
break;
case 'clubs':
$suit = 'Clubs';
break;
case 'diamonds':
$suit = 'Diamonds';
break;
case 'spades':
$suit = 'Spades';
break;
case 'heart':
$suit = 'Hearts';
break;
case 'club':
$suit = 'Clubs';
break;
case 'diamond':
$suit = 'Diamonds';
break;
case 'spade':
$suit = 'Spades';
break;
default:
$suit = 'Hearts';
}
switch ($val) {
case 1:
$cardVal = "Ace";
break;
case 2:
$cardVal = "2";
break;
case 3:
$cardVal = "3";
break;
case 4:
$cardVal = "4";
break;
case 5:
$cardVal = "5";
break;
case 6:
$cardVal = "6";
break;
case 7:
$cardVal = "7";
break;
case 8:
$cardVal = "8";
break;
case 9:
$cardVal = "9";
break;
case 10:
$cardVal = "10";
break;
case 11:
$cardVal = "Jack";
break;
case 12:
$cardVal = "Queen";
break;
case 13:
$cardVal = "King";
break;
case '1':
$cardVal = "Ace";
break;
case '2':
$cardVal = "2";
break;
case '3':
$cardVal = "3";
break;
case '4':
$cardVal = "2";
break;
case '5':
$cardVal = "3";
break;
case '6':
$cardVal = "2";
break;
case '7':
$cardVal = "3";
break;
case '8':
$cardVal = "2";
break;
case '9':
$cardVal = "3";
break;
case '10':
$cardVal = "2";
break;
case 'jack':
$cardVal = "Jack";
break;
case 'queen':
$cardVal = "Queen";
break;
case 'king':
$cardVal = "King";
break;
case 'j':
$cardVal = "Jack";
break;
case 'q':
$cardVal = "Queen";
break;
case 'k':
$cardVal = "King";
break;
default:
$cardVal = "Ace";
}
$this->setImageName();
}
function SetImageName() {
global $imageName, $cardVal, $suit;
$imageName = $cardVal . "Of" . $suit . ".gif";
}
public function GetImageName() {
global $imageName;
return $imageName;
}
public function GetSuit() {
global $suit;
return $suit;
}
public function GetCardVal() {
global $cardVal;
return $cardVal;
}
}
?>
Deck.php
<?php
class Deck {
private static $instance;
public $deck = array();
// The singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
protected function __construct() {
global $instance;
$suits = array("clubs", "spades", "hearts", "diamonds");
settype($instance->deck, "array");
foreach ($suits as $suit) {
for ($i = 1; $i <= 13; $i++) {
$card = new Card($suit, $i);
$instance->deck[] = clone $card;
}
}
}
function PrintDeck() {
global $instance;
foreach ($instance->deck as $card) {
echo $card->GetImageName() . '<br>';
}
}
}
?>
打電話來自:
<?php
include './models/Deck.php';
include './models/Card.php';
$deck = Deck::singleton();
$deck->printDeck();
?>
謝謝。可能有些愚蠢或我很愚蠢。
爲什麼使用'clone'而不是直接將新卡添加到陣列? – newfurniturey 2014-09-22 14:17:08
粘貼類Deck的定義(你是否聲明瞭'static'屬性''instance'')和你的卡片構造函數(以防萬一) – Kleskowy 2014-09-22 14:20:00
在'Card'和'Deck'類中顯示你的代碼。 @newfurniturey也是對的。在這裏克隆對象沒有任何意義。將卡直接添加到陣列('$ instance-> deck [] =新卡($ suit,$ i);')會更便宜,速度更快。 – 2014-09-22 18:38:45