如何将 php isset 与函数一起使用?

How to use php isset with a function?

以下 IF 语句导致 ZendFramework 出错(空白屏幕)。 PhpStorm 在 ('page_reference') 之后用红色指示器指示 预期变量 。有没有一种方法可以在不引起错误的情况下执行此 IF 语句?

if ( isset( $this->params()->fromRoute('page_reference') ) ) {

} else {

}

正如您在 php documentation isset 中看到的那样,意味着要在可变上下文中使用。

Warning isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

在这种情况下,可以通过一种简单的方式解决这个问题:

if ( $this->params()->fromRoute('page_reference') !== null ) {

} else {

}

注意,second parameter of fromRoute$default = null,当'page_reference'没有定义时,可以用来传递期望值。