【问题标题】:Function is not returning the value函数没有返回值
【发布时间】:2017-06-25 04:44:15
【问题描述】:

我在我的控制器中调用函数test() 并且test() 函数检查是否按下了提交按钮。如果提交按钮被按下,那么它将调用模型函数。模型函数应该返回一个值,但它没有。在我的控制器文件中,它应该是echo test();,但它没有。

这是我的代码:

控制器文件:

<?php
class Controller {

    include "model.php";

    private $model;

    public function __construct() {
        $this->model = new Model();

        echo test();
    }

    public function test() {
        if($_POST['submit']) {
            $this->model->returnThisValue();
        }
    }
}
?>

模型文件:

<?php
class Model {

    public function returnThisValue() {
        return "hello";
    }
}
?>

【问题讨论】:

  • 看起来您正在尝试创建 Model 类的实例,但您没有匹配的类,只有一个函数
  • “test() 函数检查提交按钮是否被按下。如果提交按钮被按下” - 那么,我们怎么知道这没有失败? Model 类的其余代码在哪里?
  • @AndyC 对不起,帖子中的错字。 Model 上课了
  • @Fred-ii- 提交按钮被按下
  • 您期望发生什么?你没有对$this-&gt;model-&gt;returnThisValue();的结果做任何事情

标签: php function model-view-controller model controller


【解决方案1】:

把你的函数改成

public function test() {
    if($_POST['submit']) {
        return $this->model->returnThisValue();
    }
}

这会将值返回给调用方法,并允许您对它执行一些操作,例如将其回显到输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2014-02-27
    相关资源
    最近更新 更多