对 php 中的抽象类感到困惑

  • 本文关键字:抽象类 php php json
  • 更新时间 :
  • 英文 :


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

控制器.php

namespace Controller;
    use LatteEngine;
    use ModelJson;
    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;
        }
    }

和 API 控制器.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 显示"找不到方法"?我做错了什么?

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;
    }

最新更新