Is method signature in PHP a MUST or SHOULD?
Asked Answered
G

2

2

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

  // ...
}
Grubman answered 29/1, 2010 at 9:41 Comment(2)
Not yet,the framework will take days to learn.But I'm curious about the resultGrubman
@unknown: But you could build a simple test case with just two simple classes like: class A{} class B{public static function foo(A $obj){}} B::foo(new A()); B::foo("not A");Imbalance
S
1

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
Stabilize answered 29/1, 2010 at 9:42 Comment(1)
What do you mean by recoverable?Grubman
I
5

See the chapter on TypeHinting in the PHP Manual

If $request is not a sfWebRequest instance or subclass thereof or implementing an interface of this name, the method will raise a catchable fatal error. Script execution will terminate if the error is not handled.

Example

class A {}
class B extends A {}
class C {}

function foo(A $obj) {}

foo(new A);
foo(new B);
foo(new C); // will raise an error and terminate script

With interfaces

interface A {}
class B implements A {}
class C {}

function foo(A $obj) {}

foo(new B);
foo(new C); // will raise an error and terminate script
Intrusion answered 29/1, 2010 at 9:44 Comment(0)
S
1

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
Stabilize answered 29/1, 2010 at 9:42 Comment(1)
What do you mean by recoverable?Grubman

© 2022 - 2024 — McMap. All rights reserved.