Php 致命错误:未捕获错误:在注册表类的null上调用成员函数get()

Php 致命错误:未捕获错误:在注册表类的null上调用成员函数get(),php,Php,我有这个类作为注册表类: namespace App\Core; final class Registry { private $data = array(); /** * @param string $key * * @return mixed */ public function get($key) { return (isset($this->data[$key]) ? $this->

我有这个类作为注册表类:

namespace App\Core;

final class Registry {
    private $data = array();

    /**
     * @param   string  $key
     * 
     * @return  mixed
     */
    public function get($key) {
        return (isset($this->data[$key]) ? $this->data[$key] : null);
    }

    /**
     * @param   string  $key
     * @param   string  $value
     */ 
    public function set($key, $value) {
        $this->data[$key] = $value;
    }

    /**
     * @param   string  $key
     *
     * @return  bool
     */
    public function has($key) {
        return isset($this->data[$key]);
    }

}
这个主控制器:

namespace App\Core;
use App\Core\Registry;

abstract class Controller {
    protected $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function __get($key) {
        return $this->registry->get($key); //error line
    }

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}
namespace App\Catalog\Controller;

class IndexController extends \App\Core\Controller {

    public function __construct()
    {
    }

    public function index(){
        $this->db->query();
    }
}
并且需要在索引控制器中获取值:

namespace App\Core;
use App\Core\Registry;

abstract class Controller {
    protected $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function __get($key) {
        return $this->registry->get($key); //error line
    }

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}
namespace App\Catalog\Controller;

class IndexController extends \App\Core\Controller {

    public function __construct()
    {
    }

    public function index(){
        $this->db->query();
    }
}
在db类中:

namespace App\Core;

class Db {

    public function __construct()
    {
    }

    public function query(){
        echo 'Im query';
    }
}
现在我在主索引中添加:

require '../vendor/autoload.php';

$registry = new App\Core\Registry();
use App\Core\Db;
$registry->set('db', new Db());
在操作中,我需要从
Db
类打印
Im查询
,但我看到了以下错误:

致命错误:未捕获错误:在/Applications/MAMP/htdocs/mvc/application/Core/Controller.php:16堆栈跟踪:#0/Applications/MAMP/htdocs/mvc/application/Catalog/Controller/IndexController.php(13):App\Core\Controller->u get('db')#1[内部函数]:App\Catalog\Controller\IndexController->index()php(649):call_user_func_数组(数组,数组)#3/Applications/MAMP/htdocs/mvc/application/Core/System/Route.php(599):App\Core\System\Route->callback(数组,数组)#4/Applications/MAMP/htdocs/mvc/public/index.php(63):App\Core\System\Route end()#5{main}在第16行的/Applications/MAMP/htdocs/mvc/application/Core/Controller.php中抛出

注意:我使用composer PSR-4加载所有类,如下所示:

  "autoload": {
    "psr-4": {"App\\": "application/"}
  }

你怎么能在修正这个错误

在IndexController中,您已经覆盖了_构造方法。那么,您如何将注册表传递给控制器?如果只传递注册表,则将获取值。@HariKT:您的意思是从
IndexController
中删除
\uu construct
?!是,因为您没有在构造函数中执行任何操作,也没有使用注册表值调用父构造函数。您在index.php中所做的是创建注册表对象。现在在App\Core\System\Route上,您可以将此注册表传递给它。我这里没有更多的代码告诉你。但是调试每一行,这将帮助您探索如何解决这个问题。如果你有时间,我建议你读书。