问题
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();
}
// ...
}
回答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
回答2:
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
来源:https://stackoverflow.com/questions/2161040/is-method-signature-in-php-a-must-or-should