简体   繁体   English

PHP类中的对象数组

[英]Array of objects within class in 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. 这样,我意识到我希望对象数组成为另一个类的成员。

Edit: 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? 因此,我的问题是:如何在类LogFile中有一个包含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”;

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 ? 换句话说,我需要在LogFile类中创建包含Match类型对象的数组吗?

This is an addition to the answer by Brad or by swatkins . 这是Bradswatkins答案 的补充 You wrote: 你写了:

what do I need to do in class LogFile to create an array that contains objects of type Match? 我需要在LogFile类中做什么以创建包含Match类型对象的数组?

You can create an "array" that only can contain Match objects. 您可以创建一个只能包含Match对象的“数组”。 This is fairly easy by extending from ArrayObject and only accepting object of a specific class: 通过从ArrayObject扩展并仅接受特定类的对象,这相当容易:

class Matches extends ArrayObject
{
    public function offsetSet($name, $value)
    {
        if (!is_object($value) || !($value instanceof Match))
        {
            throw new InvalidArgumentException(sprintf('Only objects of Match allowed.'));
        }
        parent::offsetSet($name, $value);
    }
}

You then make you class LogFile use that Matches class: 然后,使您的LogFile类使用Matches类:

class LogFile
{
    public $formattedMatches;
    public $pathToLog;
    public $matchCount;
    public $matches;
    public function __construct()
    {
        $this->matches = new Matches();
    }
}

In the constructor you set it up, the new Matches "Array". 在构造函数中,您将其设置为新的Matches “ Array”。 Usage: 用法:

$l = new LogFile();
$l->matches[] = new Match(); // works fine

try
{
    $l->matches[] = 'test'; // throws exception as that is a string not a Match
} catch(Exception $e) {
    echo 'There was an error: ', $e->getMessage();

}

Demo - Hope this is helpful. 演示 -希望这会有所帮助。

Just create another public variable for matches. 只需为匹配创建另一个公共变量。 Then, you can initialize it as an array in the constructor method . 然后,可以在构造方法中将其初始化为数组。

class LogFile
{
    public $formattedMatches;
    public $pathToLog;
    public $matchCount;
    public $matches;

    function __construct() {
        $matches=array();
        //Load $matches with whatever here
    }
}
class LogFile
{
    public $formattedMatches;
    public $pathToLog;
    public $matchCount;
    public $matches = array();
}

PHP isn't strongly typed - you can put whatever you like in any variable. PHP的类型不是很严格-您可以在变量中添加任何内容。 To add to matches, just do $logFile->matches[] = new Match(); 要添加到匹配项中,只需执行$logFile->matches[] = new Match(); .

Yeah, that would work. 是的,那行得通。

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();

Just include 只包括

public $matches = array();

Then when you want to add to the the array: 然后,当您想添加到数组中时:

$matches[] = $match;   // $match being object of type match

您可以使用SplObjectStorage对象,因为它旨在存储对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM