如何在 PHP 中使用 array_shift() 返回对象?
我正在构建一些使用扑克牌的课程。我有一个 Card 类和一个 Deck 类。我想通过在 Card 对象数组上使用 array_shift() 来实现从牌组中绘制一张牌;该数组是 Deck 的属性。这是类的代码,存储在文件“cardlib.php”中:
<?php
class Card
{
private $suit="";
private $face="";
function __construct($suit,$face){
$this->suit=$suit;
$this->face=$face;
}
public function getSuit(){
return $suit;
}
public function getFace(){
return $face;
}
public function display(){
echo $this->suit.$this->face;
}
}
class Deck
{
private $suits=array("S","H","C","D");
private $faces=array("2","3","4","5",
"6","7","8","9","10",
"J","Q","K","A");
private $stack=array();
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$stack[] = $card;
}
}
}
public function doShuffle(){
shuffle($this->stack);
}
public function draw(){
$card = array_shift($this->stack);
var_dump($card);
return $card;
}
}
?>
这是测试代码,在“index.php”中:
<?php
include_once "cardlib.php";
$myDeck=new Deck();
$myDeck->doshuffle();
$card=$myDeck->draw();
$card->display();
?>
测试代码给出以下错误消息:
NULL 致命错误:第 6 行在 C:\wamp\www\cardgames\index.php 中的非对象上调用成员函数 display()
似乎 array_shift() 没有返回对卡对象的引用,或者我没有使用 array_shift() 返回的内容正确初始化 $card 变量。如何获得我想要的对象?
I'm building some classes for working with playing cards. I have a Card class and a Deck class. I want to implement drawing a card from the deck by using array_shift() on an array of Card objects; this array is a property of Deck. Here is the code for the classes, which is stored in the file "cardlib.php":
<?php
class Card
{
private $suit="";
private $face="";
function __construct($suit,$face){
$this->suit=$suit;
$this->face=$face;
}
public function getSuit(){
return $suit;
}
public function getFace(){
return $face;
}
public function display(){
echo $this->suit.$this->face;
}
}
class Deck
{
private $suits=array("S","H","C","D");
private $faces=array("2","3","4","5",
"6","7","8","9","10",
"J","Q","K","A");
private $stack=array();
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$stack[] = $card;
}
}
}
public function doShuffle(){
shuffle($this->stack);
}
public function draw(){
$card = array_shift($this->stack);
var_dump($card);
return $card;
}
}
?>
And here is the test code, in "index.php":
<?php
include_once "cardlib.php";
$myDeck=new Deck();
$myDeck->doshuffle();
$card=$myDeck->draw();
$card->display();
?>
The test code gives me the following error message:
NULL
Fatal error: Call to a member function display() on a non-object in C:\wamp\www\cardgames\index.php on line 6
It seems that array_shift() isn't returning the reference to the card object, or I'm not properly initializing the $card variable with what array_shift() returns. How do I get the object that I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在构造函数中,您将堆栈存储在局部变量中。使用
$this->stack
将其存储在成员变量中。In the constructor, you store the stack in a local variable. Use
$this->stack
to store it in the member variable.在
Deck::__construct()
中,使用$this->stack[] = ..
而不是$stack[] = ..
In
Deck::__construct()
, use$this->stack[] = ..
instead of$stack[] = ..