PHP中的类中的对象数组 [英] Array of objects within class in PHP

查看:129
本文介绍了PHP中的类中的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近意识到,使用更好/更具描述性的对象,我的当前方法将大大改善。因此,我意识到我想要一个对象数组是另一个类的成员。

I recently realized my currently approach on a project would greatly improve with the use of better/more descriptive objects. As such, I realized that I want an array of objects to be a member of another class.

编辑:我不清楚我的问题是什么。我的问题是:我如何在类LogFile中包含对象类型Match的数组?

I wasn't clear as to what my question was. My question is thus: How do I have an array in class LogFile that contains objects of type Match?

class LogFile
{
    public $formattedMatches;
    public $pathToLog;
    public $matchCount;
    ** An array called matches that is an array of objects of type Match **
}

class Match
{
    public $owner;
    public $fileLocation;
    public $matchType;
}

最后我想要能够做的事情:

Eventually I want to be able to do something like:

$logFile = new LogFile();
$match = new Match();
$logFile->matches[$i]->owner = "Brian";

我如何做我所描述的?换句话说,我需要在类 LogFile 中创建一个包含 Match 类型的对象的数组?

How do I do what I described? In other words, what do I need to do in class LogFile to create an array that contains objects of type Match?

推荐答案

是的,这将工作。

class LogFile
{
    public $formattedMatches;
    public $pathToLog;
    public $matchCount;
    public $matches = array();
}

class Match
{
    public $owner;
    public $fileLocation;
    public $matchType;
}

$l = new LogFile();
$l->matches[0] = new Match();

这篇关于PHP中的类中的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆