Array of objects within class in PHP

后端 未结 6 1921
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 04:52

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 obj

相关标签:
6条回答
  • 2020-12-22 05:12

    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();
    
    0 讨论(0)
  • 2020-12-22 05:15

    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.

    0 讨论(0)
  • 2020-12-22 05:15

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

    0 讨论(0)
  • 2020-12-22 05:22
    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();.

    0 讨论(0)
  • 2020-12-22 05:32

    Just include

    public $matches = array();
    

    Then when you want to add to the the array:

    $matches[] = $match;   // $match being object of type match
    
    0 讨论(0)
  • 2020-12-22 05:33

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题