1

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?

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?

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 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? –  Oct 18 '11 at 18:59
  • Instead of commenting on all of the individual answers I just wanted to say thanks to all of you for your help. I see how it is now. The lack of strong formatting rules throws me off still. –  Oct 18 '11 at 19:08

7 Answers7

4

This is an addition to the answer by Brad or by swatkins. You wrote:

what do I need to do in class LogFile to create an array that contains objects of type Match?

You can create an "array" that only can contain Match objects. This is fairly easy by extending from ArrayObject and only accepting object of a specific class:

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:

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". 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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
2

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();
swatkins
  • 13,530
  • 4
  • 46
  • 78
1

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
    }
}
Brad
  • 159,648
  • 54
  • 349
  • 530
1
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. To add to matches, just do $logFile->matches[] = new Match();.

WWW
  • 9,734
  • 1
  • 29
  • 33
0

Create the array as public static:

public static $arr = array();

And then just push each new instance of the class into the array as they are constructed by adding the following line to your constructor:

array_push(yourClassHere:: $arr, $this);

You can then access the array globally, for instance:

print_r(YourClass:: $arr);

This will present you with an array of all objects created for that class.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Just include

public $matches = array();

Then when you want to add to the the array:

$matches[] = $match;   // $match being object of type match
endyourif
  • 2,186
  • 19
  • 33
0

You could use an object of SplObjectStorage as this is intended to store objects.

str
  • 42,689
  • 17
  • 109
  • 127