Search code examples
phpmethod-signature

Is method signature in PHP a MUST or SHOULD?


I mean if it's called with $request which is not instance of sfWebRequest ,will it be fatal,or just a warning?

class jobActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->jobeet_job_list = Doctrine::getTable('JobeetJob')
      ->createQuery('a')
      ->execute();
  }

  // ...
}

Solution

  • It will be a catchable fatal error.

    Here is an example:

    class MyObj {}
    
    function act(MyObj $o)
    {
        echo "ok\n";
    }
    
    function handle_errors($errno, $str, $file, $line, $context)
    {
        echo "Caught error " . $errno . "\n";
    }
    
    set_error_handler('handle_errors');
    
    act(new stdClass());
    /* Prints                                                                       
     *                                                                              
     * Caught error 4096                                                            
     * ok                                                                           
     */
    

    If there wasn't a set_error_handler call the code will fail with an error:

    Catchable fatal error: Argument 1 passed to act() must be an instance of MyObj, 
    instance of stdClass given, called in /home/test/t.php on line 16 and defined in
    /home/test/t.php on line 4