我试图在PHP中创建一个对象的数组(i am trying to make a array of a object in php)

编程入门 行业动态 更新时间:2024-10-07 10:17:38
我试图在PHP中创建一个对象的数组(i am trying to make a array of a object in php)

我正在尝试制作一组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.

更多推荐

本文发布于:2023-07-27 04:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   创建一个   对象   PHP   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!