php - 连续两次返回
问题描述
在查看某人的代码时,我在几个课程中连续遇到两个返回。例如:
class class1{
private $property1;
final function __construct($property1){
$this->property1 = $property1;
}
private $property2 = true;
function method1($bool){
$this->property2 = $bool;
return $this;
return new class1();
}
它是如何工作的,这个结构是做什么用的?
解决方案
PHP 只允许一个返回语句。第一次返回之后的所有内容都将被忽略。在你的情况下,return new class1();
永远不会被调用。
来自 PHP 文档:
如果从函数内部调用,return 语句会立即结束当前函数的执行,并将其参数作为函数调用的值返回。return 还结束 eval() 语句或脚本文件的执行。
您可以 在 php.net上查看更多信息
推荐阅读
- c - 如何理解 valgrind 输出的内存泄漏?
- types - 有没有办法在 OCaml 类型系统中嵌入单元处理逻辑?
- c# - 如何装饰 EF Core 的 DbContext
- user-interface - Highcharts - 在折线图的工具提示中显示字符串类别标签
- java - Cassandra 3.11 及更高版本的外部 jar 支持的替代方案
- flutter - 在flutter_treeview中如何改变选定节点的颜色
- jquery - 引导模式弹出窗口的按钮不调用动作控制器
- python - 从范围创建 csv 列以生成 lat 和 lng
- java - 获取寻址多项式以将该矩阵存储在一维数组中
- android-studio - 我正在尝试从评级中获取数字并将它们相加以计算平均值,但它会单独打印详细信息