Can you pass more parameters than a function expects?

前端 未结 2 2033
无人共我
无人共我 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: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();
    }
    

提交回复
热议问题