我正在尝试制作一组Spell 。
我目前的代码
class Spell { public $bomb = 0; public $fire = 0; function Spell() { $this->bomb =0; $this->fire =0; } }我在这个游戏类中声明了对象拼写
class game { public $Spell=array(); function Game() { $this->Spell[0] = new Spell(); } function s() { $this->Spell[1]->$bomb = $load($x) $this->Spell[1]->$fire = $load($x); $this->Spell[2]->$bomb = $load($y) $this->Spell[3]->$bomb = $load($z) } }它返回此错误 - Warning: Creating default object from empty value in...我想这不是创建对象数组的最佳方法。 怎么做得好?
编辑:xyz,只返回字符串
I'm trying to make an array of Spells.
My current code
class Spell { public $bomb = 0; public $fire = 0; function Spell() { $this->bomb =0; $this->fire =0; } }And I declare the object spell on my game class like this
class game { public $Spell=array(); function Game() { $this->Spell[0] = new Spell(); } function s() { $this->Spell[1]->$bomb = $load($x) $this->Spell[1]->$fire = $load($x); $this->Spell[2]->$bomb = $load($y) $this->Spell[3]->$bomb = $load($z) } }It returns this error -- Warning: Creating default object from empty value in... I guess this isn't the best way to create an array of objects. How to do it properly?
EDIT: x y z, just return strings
最满意答案
问题是你没有为$ this-> Spell [1],$ this-> Spell [2]和$ this-> Spell [3]创建对象。 如果您将Game()构造函数更改为:
function Game() { for ($i = 1; $i <= 3; $i++) { $this->Spell[$i] = new Spell(); } }应该可以正常工作。
The problem is that you have not created objects for $this->Spell[1], $this->Spell[2] and $this->Spell[3]. If you change your Game() constructor to this:
function Game() { for ($i = 1; $i <= 3; $i++) { $this->Spell[$i] = new Spell(); } }It should probably work fine.
更多推荐
发布评论