Can you pass more parameters than a function expects?

前端 未结 2 2011
无人共我
无人共我 2021-01-11 14:36

If you have two classes extending the same base class, and need to make one of them override a function to take an additional parameter. Is it okay to always pass the additi

相关标签:
2条回答
  • 2021-01-11 15:26

    Yes you can. Gather them with func_get_args()

    0 讨论(0)
  • 2021-01-11 15:33

    There are two ways to do this:

    Extend the methods to use default parameters:

    public function doSomething($input=null) {}
    

    Note that you'll need to do this for all instances, even if it isn't used.

    In this case, you may want to use the arguments array of the method:

    public function doSomething() {
      $args = func_get_args();
      if (isset($args[0])) { $this->doSomethingElse();
    }
    
    0 讨论(0)
提交回复
热议问题