PHP foreach 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2384760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP foreach over an array of objects
提问by Joshua Lowry
I'm trying to use a foreach loop for an array of objects. Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. Unfortunately, the web browser shows I have an error: Fatal error: Call to a member function BattleInitiated() on a non-object in /nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/html/Battle.php on line 75
我正在尝试对一组对象使用 foreach 循环。在 BeginBattle() 方法内部,我想遍历所有对象并自动增加它们的播放次数。不幸的是,网络浏览器显示我有一个错误:致命错误:在 /nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/html/Battle 中的非对象上调用成员函数 BattleInitiated()第 75 行的 .php
Any ideas?
有任何想法吗?
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Battle
*
* @author joshualowry
*/
class Battle {
/**
*
* @var <type>
*/
private $_players;
/**
*
* @var <type>
*/
private $_battleInProgress;
/**
*
*/
public function Battle(){
$this->_players = array();
$this->_battleInProgress = FALSE;
}
/**
*
* @param <type> $player
* @return <type>
*/
public function AddPlayer($player){
if(!$this->_battleInProgress)
$this->_players[] = $player;
else
return;
//Spit some error
}
/**
*
* @param <type> $player
* @return <type>
*/
public function BattleWon($player){
if($player != NULL)
$player->BattleWon();
else
return;
//Spit some error
}
/** GetPlayerByName Get the player's object by the player's name field.
*
* @param <type> $playerName
* @return <type>
*/
public function GetPlayerByName($playerName){
foreach($this->_players as &$player) {
if($player->GetName() == $playerName)
return $player;
}
return NULL;
}
/**
*
*/
public function BeginBattle(){
$this->_battleInProgress = TRUE;
foreach($this->_players as $player){
$player->BattleInitiated();
}
}
/**
*
*/
public function DisplayCurrentBoard() {
echo "Name Alias Wins Battles<br/>";
foreach($this->_players as &$player){
echo "$player->GetName() $player->GetAlias() $player->GetWins() $player->GetBattles()<br/>";
}
}
}
?>
This is where everything is declared and called:
这是声明和调用所有内容的地方:
<?php
include 'Battle.php';
include 'Person.php';
include 'Player.php';
$currentBattle = new Battle();
$playerA = new Player("JohnnyDanger","John",0,0);
$playerB = new Player("JoshTheJest","Josh",0,0);
$PlayerC = new Player("CarbQueen","Nicole",0,0);
$currentBattle->AddPlayer($playerA);
$currentBattle->AddPlayer($playerB);
$currentBattle->AddPlayer($playerC);
$currentBattle->BeginBattle();
$currentBattle->BattleWon($currentBattle->GetPlayerByName("Josh"));
$currentBattle->DisplayCurrentBoard();
?>
The Player Class
玩家类
<?php
/**
* Description of Player
*
* @author joshualowry
*/
class Player extends Person {
private $_alias;
private $_wins;
private $_battles;
public function Player($name, $alias, $wins, $battles) {
parent::SetName($name);
$this->_alias = $alias;
$this->_battles = $battles;
if($battles == 0) {
$this->_wins = 0;
}
else {
$this->_wins = $wins;
}
}
protected function SetAlias($value){
$this->_alias = $value;
}
public function GetAlias(){
return $this->_alias;
}
protected function SetBattles($value) {
$this->_battles = $value;
}
public function GetBattles(){
return $this->_battles;
}
protected function SetWins($value) {
$this->_wins = $value;
}
public function GetWins() {
return $this->_wins;
}
public function BattleWon(){
$this->_wins += 1;
}
public function BattleInitiated(){
$this->_battles += 1;
}
}
?>
回答by Pascal MARTIN
The error message indicates that you are trying to all the BattleInitiated()method on something that wasn't an object.
错误消息表明您正在尝试对BattleInitiated()不是对象的事物执行所有方法。
Judging from your code, the problem seems to be with this loop, in the BeginBattle()method :
从你的代码来看,问题似乎出在这个循环中,在BeginBattle()方法中:
foreach($this->_players as $player){
$player->BattleInitiated();
}
Which means $player, at least one in your array, is probably not an object ; maybe it's null, or an array ?
这意味着 $player,至少在你的数组中,可能不是一个对象;也许是null,或者是一个数组?
To know more, you should use var_dumpto display the content of $this->_playersbefore the loop, just to make sure it contains what you expect it to :
要了解更多信息,您应该使用var_dump来显示$this->_players循环之前的内容,以确保它包含您期望的内容:
public function BeginBattle(){
var_dump($this->_players);
$this->_battleInProgress = TRUE;
foreach($this->_players as $player){
$player->BattleInitiated();
}
}
If $this->_playersdoesn't contain what you expect it to (and it probably doesn't !), you'll then have to find out why...
如果$this->_players不包含您期望的内容(并且可能不包含!),那么您必须找出原因......
Considering $this->_playersis modified by the AddPlayer()method, which adds what it receives to the end of the array, I would bet that AddPlayer()is called at least once without a correct $playeras a parameter.
考虑到$this->_players被AddPlayer()方法修改,它将它接收到的内容添加到数组的末尾,我敢打赌它AddPlayer()至少被调用一次而没有正确$player的参数。
To help with that, you could use var_dumpon the $playerbeing added :
为了帮助解决这个问题,您可以var_dump在$player添加的内容上使用:
public function AddPlayer($player){
var_dump($player);
if(!$this->_battleInProgress)
$this->_players[] = $player;
else
return;
//Spit some error
}
If that var_dumpindicates at least once that $playeris not an object (for instance, it's null, or an array, or a string, ...), that's the cause of your Fatal Error.
如果这var_dump表明至少有一次它$player不是对象(例如,它是null,或数组,或字符串,...),那就是导致致命错误的原因。
回答by itsid
don't you see it??
你没看到吗??
it's all because of a small typo:
这都是因为一个小错字:
$playerA = new Player("JohnnyDanger","John",0,0);
$playerB = new Player("JoshTheJest","Josh",0,0);
$PlayerC = new Player("CarbQueen","Nicole",0,0);
$currentBattle->AddPlayer($playerA);
$currentBattle->AddPlayer($playerB);
$currentBattle->AddPlayer($playerC);
declared: $_P_layerC used: $_p_layerC
声明:$_P_layerC 使用:$_p_layerC
correct that and you're good to go
纠正这一点,你很高兴去
回答by Jonathan Czitkovics
Your $playervariable is either nullor not an Objectof the typeyou want it to be.
您的$player变量要么为空,要么不是您想要的类型的对象。
PlayerObjectis what ever your class name for player is.
PlayerObject是你的玩家类名是什么。
For example
例如
$battle=new Battle();
$player1=new PlayerObject();
$player2="player";
$battle->AddPlayer($player1);
$battle->AddPlayer($player2);
$battle->BeginBattle();
when you call BeginBattle()the $player1->BattleInitiated();will be successful but the $player2->BattleInitiated()will give you the fatal error and stop your code from running. same if $player2was null, an integer or something that is not PlayerObject.
当你打电话BeginBattle()的$player1->BattleInitiated();一定会成功,但$player2->BattleInitiated()会给你致命错误,停止运行代码。如果$player2为空、整数或不是 的东西,则相同PlayerObject。

