Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中返回带有数组_shift()的对象?_Php_Arrays_Object - Fatal编程技术网

如何在PHP中返回带有数组_shift()的对象?

如何在PHP中返回带有数组_shift()的对象?,php,arrays,object,Php,Arrays,Object,我在建一些打牌的课程。我有纸牌课和套牌课。我想通过在卡片对象数组上使用array_shift()实现从卡片组中绘制卡片;此数组是Deck的属性。下面是存储在文件“cardlib.php”中的类的代码: 下面是“index.php”中的测试代码: 测试代码给出了以下错误消息: 空的 致命错误:对C:\wamp\www\cardgames\index.php第6行的非对象调用成员函数display() 似乎array_shift()没有返回对card对象的引用,或者我没有用array_shi

我在建一些打牌的课程。我有纸牌课和套牌课。我想通过在卡片对象数组上使用array_shift()实现从卡片组中绘制卡片;此数组是Deck的属性。下面是存储在文件“cardlib.php”中的类的代码:


下面是“index.php”中的测试代码:


测试代码给出了以下错误消息:

空的 致命错误:对C:\wamp\www\cardgames\index.php第6行的非对象调用成员函数display()


似乎array_shift()没有返回对card对象的引用,或者我没有用array_shift()返回的值正确初始化$card变量。如何获取所需的对象?

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

function __construct(){
   foreach ($this->suits as $suit){
       foreach ($this->faces as $face){
           $card = new Card($suit,$face);
           $this->stack[] = $card;
       }
   }
}
Deck::u construct()
中,使用
$this->stack[]=..
而不是
$stack[]=..

<?php
include_once "cardlib.php";
$myDeck=new Deck();
$myDeck->doshuffle();
$card=$myDeck->draw();
$card->display();

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