【发布时间】:2015-03-02 18:52:16
【问题描述】:
今天早些时候,我向this question 询问了有关重新调整 switch 语句的输出的问题。
结论是 switch 语句中的 return 应该只在函数内部使用。
现在我正在尝试在类中的方法中使用返回。 这是代码:
class ClassName{
public function www($foo)
{
switch ($foo) {
case 1:
return '1';
break;
case 2:
return '2';
break;
default:
return 'no matching values were sent to the function';
break;
}
}
}
$foo = 2;
ClassName::www($foo);
当我用 echo 替换返回时,我得到一个输出。 为什么它不能与 return 一起使用?
【问题讨论】:
-
连同答案,你不需要那些
breaks,因为 return 退出了开关和功能。
标签: php oop switch-statement return