【发布时间】:2010-03-05 05:38:03
【问题描述】:
我正在尝试对对象数组使用 foreach 循环。在 BeginBattle() 方法中,我想遍历所有对象并自动增加它们的播放次数。不幸的是,网络浏览器显示我有一个错误:致命错误:在 /nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/ 中的非对象上调用成员函数 BattleInitiated() html/Battle.php 在第 75 行
有什么想法吗?
<?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/>";
}
}
}
?>
这是声明和调用所有内容的地方:
<?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();
?>
播放器类
<?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;
}
}
?>
【问题讨论】:
-
告诉我们在哪里调用 AddPlayer。
-
你传递给 AddPlayer() 什么?
-
您如何实例化您的
Battle()对象并调用AddPlayer()和BeginBattle()函数? -
你有一个 Player 类和一个合适的构造函数 [php.net/manual/en/language.oop5.decon.php] ?
-
@JannieT 您的链接给出了 404。
标签: php arrays foreach iteration