如何在 PHP 中使用 array_shift() 返回对象?

发布于 2024-10-18 12:45:10 字数 1441 浏览 0 评论 0原文

我正在构建一些使用扑克牌的课程。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-10-25 12:45:10

在构造函数中,您将堆栈存储在局部变量中。使用$this->stack将其存储在成员变量中。

function __construct(){
   foreach ($this->suits as $suit){
       foreach ($this->faces as $face){
           $card = new Card($suit,$face);
           $this->stack[] = $card;
       }
   }
}

In the constructor, you store the stack in a local variable. Use $this->stack to store it in the member variable.

function __construct(){
   foreach ($this->suits as $suit){
       foreach ($this->faces as $face){
           $card = new Card($suit,$face);
           $this->stack[] = $card;
       }
   }
}
活雷疯 2024-10-25 12:45:10

Deck::__construct() 中,使用 $this->stack[] = .. 而不是 $stack[] = ..

In Deck::__construct(), use $this->stack[] = .. instead of $stack[] = ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文