对php

时间:2017-10-18 08:34:12

标签: php json

我正在构建自己的小框架来学习MVC。我有像这样的控制器

Controller.php

namespace Controller;

    use Latte\Engine;
    use Model\Json;

    abstract class Controller
    {
        protected $model;
        protected $latte;
        protected $json;
        protected $args = array();

        public function __construct(Engine $latte, Json $json, $model, array $args)
        {
            $this->latte = $latte;
            $this->model = $model;
            $this->args = $args;
        }
    }

和ApiController.php

namespace Controller;

class CoinflipApiController extends Controller
{
    function index()
    {
    }

    public function getGames()
    {
        try {
            $games = $this->model->getGames();
            $this->json->generate($games);
        } catch (Exception $e) {
            $this->json->generate(array('error'=>$e->errorInfo[2]), Json::ERROR);
        }
    }
}

为什么调用$this->json->generate()不起作用,IDE显示'找不到方法'?我做错了什么?

1 个答案:

答案 0 :(得分:3)

Controller类的构造函数不使用Json $json。看起来应该是这样的。

    public function __construct(Engine $latte, Json $json, $model, array $args)
    {
        $this->latte = $latte;
        $this->model = $model;
        $this->json  = $json;
        $this->args  = $args;
    }