PHP 中的方法签名是必须还是应该?

发布于 2024-08-19 18:08:09 字数 330 浏览 2 评论 0原文

我的意思是,如果使用不是 sfWebRequest 实例的 $request 调用它,它会是致命的,还是只是一个警告?

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

  // ...
}

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

  // ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-08-26 18:08:09

请参阅 PHP 手册中有关 TypeHinting 的章节

如果 $ request 不是 sfWebRequest 实例或其子类实现此名称的接口,该方法将引发 可捕获的致命错误。如果不处理错误,脚本执行将终止。

示例

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

带接口的

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

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
感情废物 2024-08-26 18:08:09

这将是一个可捕获的致命错误。

下面是一个示例:

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                                                                           
 */

如果没有 set_error_handler 调用,代码将失败并出现错误:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文