【问题标题】:Array of objects within class in PHPPHP中类内的对象数组
【发布时间】:2011-12-10 08:43:38
【问题描述】:

我最近意识到,如果使用更好/更具描述性的对象,我目前的项目方法会大大改进。因此,我意识到我希望一个对象数组成为另一个类的成员。

编辑:我不清楚我的问题是什么。因此,我的问题是:如何在 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;
}

最终我希望能够做类似的事情:

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

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

【问题讨论】:

  • 我该怎么做?换句话说,我需要在类 LogFile 中做什么来创建一个包含 Match 类型的对象的数组?
  • 我不想评论所有个人的答案,我只想对大家的帮助表示感谢。我看现在怎么样了。缺乏强大的格式规则让我仍然感到厌烦。

标签: php class


【解决方案1】:

将数组创建为公共静态:

public static $arr = array();

然后只需将类的每个新实例推入数组,因为它们是通过在构造函数中添加以下行来构造的:

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

然后您可以全局访问该数组,例如:

print_r(YourClass:: $arr);

这将为您提供为该类创建的所有对象的数组。

【讨论】:

    【解决方案2】:

    这是对answer by Bradby swatkins 的补充。你写道:

    我需要在 LogFile 类中做什么来创建一个包含 Match 类型对象的数组?

    您可以创建一个只能包含Match 对象的“数组”。通过从 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);
        }
    }
    

    然后你让 LogFile 类使用 Matches 类:

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

    在您设置的构造函数中,新的Matches“数组”。用法:

    $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 - 希望对您有所帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        只需包含

        public $matches = array();
        

        那么当你要添加到数组时:

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

        【讨论】:

          【解决方案5】:

          是的,那会奏效。

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

          【讨论】:

            【解决方案6】:
            class LogFile
            {
                public $formattedMatches;
                public $pathToLog;
                public $matchCount;
                public $matches = array();
            }
            

            PHP 不是强类型的——你可以将任何你喜欢的东西放在任何变量中。要添加到匹配项,只需执行$logFile->matches[] = new Match();

            【讨论】:

              【解决方案7】:

              只需为匹配创建另一个公共变量。然后,可以将其初始化为constructor method中的数组。

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2017-02-17
                • 1970-01-01
                • 2013-09-28
                • 2012-08-15
                • 2016-06-26
                • 2016-02-02
                • 1970-01-01
                相关资源
                最近更新 更多