【问题标题】:PHP Function Arguments: Array of Objects of a Specific ClassPHP 函数参数:特定类的对象数组
【发布时间】:2011-09-25 06:27:04
【问题描述】:

我有一个接受特定类成员的函数:

public function addPage(My_Page $page)
{
  // ...
}

我想创建另一个函数,该函数采用 My_Page 对象数组:

public function addPages($pages)
{
  // ...
}

我需要确保 $pages 数组的每个元素都是 My_Page 的一个实例。我可以用foreach($pages as $page) 做到这一点并检查instance of,但我可以在函数定义中以某种方式指定数组必须是 My_Page 对象的数组吗?即兴创作,例如:

public function addPages(array(My_Page)) // I realize this is improper PHP...

谢谢!

【问题讨论】:

    标签: php arrays function object arguments


    【解决方案1】:

    不,不可能直接做。你可以试试这个“聪明”的技巧:

    public function addPages(array $pages)
    {
        foreach($pages as $page) addPage($page);
    }
    
    public function addPage(My_Page $page)
    {
        //
    }
    

    但我不确定这是否值得所有的麻烦。如果 addPage 本身有用,那将是一个好方法。

    【讨论】:

      【解决方案2】:

      不要传递对象数组 (array(My_Page)),而是定义自己的类并在函数中要求它的实例:

      class MyPages { ... }
      
      public function addPages(MyPages pages)
      

      【讨论】:

        【解决方案3】:

        如果你使用这个类,你可以这样做:

        interface addPageInterface
        {
           public function someThing();
        }
        
        
        class page implements addPageInterface
        {
           public function someThing()
           {
              //for example: create a page
           }
        }
        
        
        class addPage
        {
           public function __construct(addPageInterface $page)
           {
               //do some thing...
        
              return $page; //this will return just one page
           }
        }
        
        
        class addPages
        {
           public function __construct(addPageInterface... $page)
           {
              //do some thing...
        
              return $page; //this will return an array which contains of page
           }
        }
        

        【讨论】:

        • 用正确的推理给出正确的解释。
        【解决方案4】:

        您还可以添加 PhpDoc 注释以在 IDE 中自动完成

            /**
             * @param My_Page[] $pages
             */
            public function addPages(array $pages)
            {
              // ...
            }
        

        【讨论】:

        【解决方案5】:

        从 php5.6 开始,您可以使用 splat 运算符 来输入提示所有数组元素作为函数参数。在这种情况下,您没有比存储在数组中的页面更多的参数,因此它应该可以正常工作:

        public function addPages(Page ...$pages)
        

        更多信息: https://www.php.net/manual/en/migration56.new-features.php

        https://lornajane.net/posts/2014/php-5-6-and-the-splat-operator

        【讨论】:

          【解决方案6】:
          <?php
                  
              class Page{
                      public $a;
              }
                  
              
              function addPages(Page ...$pages){
                  foreach($pages as $page){
                  
                  }
              }
                  
                  
              $pages = [];
              $pages[]  = new Page;
              $pages[]  = new Page;
              $pages[]  = new Page;
              addPages(...$pages);
              
          ?>
          

          我认为这是更好的方式

          【讨论】:

          • 请解释为什么你认为这是一个更好的方法。
          • 这会抛出一个错误:Argument #1 must be type of Page, array given.
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多