【问题标题】:ERROR: call to a member function get() on null错误:在 null 上调用成员函数 get()
【发布时间】:2017-09-14 20:37:15
【问题描述】:

我收到此错误消息:

PHP 致命错误:在第 10 行的 /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php 中调用成员函数 get() on null

控制器中的所有代码是:

<?php
abstract class Controller {
    protected $registry;

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

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

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}

我使用注册表的代码是这样的:

define("VERSION", "1.0");
define("LANGUAGE", "1");

if (is_file('./../admin/config.php')) {
   require_once('./../admin/config.php');
}

require_once(DIR_SYSTEM . 'startup.php');

$application_config = 'admin';
$registry = new Registry();

$loader = new Loader($registry);
$registry->set('load', $loader);

$config = new Config();
$config->load('default');

$config->load($application_config);
$registry->set('config', $config);
$registry->set('request', new Request());
$response = new Response();

$response->addHeader('Content-Type: text/html; charset=utf-8');
$registry->set('response', $response);

$registry->set('cache', new Cache($config->get('cache_type'), $config-
>get('cache_expire')));
$registry->set('url', new Url($config->get('site_ssl')));

$language = new Language($config->get('language_default'));
$language->load($config->get('language_default'));

$registry->set('language', $language);
$registry->set('document', new Document());

$event = new Event($registry);
$registry->set('event', $event);

if ($config->get('db_autostart')) {
   $registry->set('db', new DB($config->get('db_type'), $config-
   >get('db_hostname'), $config->get('db_username'), $config-
   >get('db_password'), $config->get('db_database'), $config-
   >get('db_port')));
}

if ($config->get('session_autostart')) {
   $session = new Session();
   $session->start();
   $registry->set('session', $session);
}

if ($config->has('action_event')) {
   foreach ($config->get('action_event') as $key => $value) {
      $event->register($key, new Action($value));
   }
}

if ($config->has('config_autoload')) {
   foreach ($config->get('config_autoload') as $value) {
     $loader->config($value);
   }
}

if ($config->has('language_autoload')) {
   foreach ($config->get('language_autoload') as $value) {
      $loader->language($value);
    }
}

if ($config->has('library_autoload')) {
   foreach ($config->get('library_autoload') as $value) {
      $loader->library($value);
   }
}

if ($config->has('model_autoload')) {
   foreach ($config->get('model_autoload') as $value) {
      $loader->model($value);
   }
}

class K2P_API_OCWRITER extends Controller
{ 

   private $errors;
private $admin;
private $adminValidated;
private $adminShops;

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

public function doLog($message)
{
    file_put_contents('./key2_log.txt', $message, FILE_APPEND);
}

public function login($usr, $pwd)
{   

    if ($this->user->login($usr, $pwd)) {
        return true;
        $this->doLog('logged in');
    } else {
        $this->doLog('Failed to login, please supply a valid 
         username/password and check your webshop url');
        die;
    }

}

public function getLanguages()
{
}

}

$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);
$registry->set('user', new Cart\User($registry));
$registry->set('tax', new Cart\Tax($registry));

$myAPI = new K2P_API_OCWRITER($registry);
$myAPI->config->set("config_language_id",LANGUAGE);
$command = $myAPI->cleanPost($_POST['command']);
$steps = $myAPI->cleanPost($_POST['steps']);
$page = $myAPI->cleanPost($_POST['page']);
$usr = $myAPI->cleanPost($_POST['usr']);
$pwd = $myAPI->cleanPost($_POST['pwd']);
//$myAPI->doLog(PHP_EOL . 'pages: ' . $page);
//$myAPI->doLog(PHP_EOL . 'steps: ' . $steps);
$totalProducts = $myAPI->getProductCount();
if ($myAPI->checkInput($usr,$pwd,$command,$page,$steps)) {
   if ($myAPI->login($usr, $pwd)) {
       switch($command){
          case "getCategoryCount":
              echo json_encode($myAPI->getCategoryCount(),JSON_FORCE_OBJECT 
              | JSON_UNESCAPED_SLASHES);
            break;
        case "getProductCount";
            echo json_encode($myAPI->getProductCount(),JSON_FORCE_OBJECT | 
            JSON_UNESCAPED_SLASHES);
            break;
        case "getCategories":
            echo json_encode($myAPI->getCategories($steps, $page, 
            JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES));
            break;
        case "getProducts":
            echo json_encode($myAPI->getProducts($steps, $page, 
            JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES));
            break;
        default:
            echo "Invalid command!";
            break;
    }
 }
}

我该如何解决?

【问题讨论】:

  • 检查$registry的值。
  • 我更新了我的问题。我在使用 $registry 的页面上添加了代码。你能看看出了什么问题。你知道如何解决它吗?
  • 子类中的 constructor 覆盖父类中的。所以父构造函数不运行也不设置this-&gt;registry
  • 我该如何解决?我可以把公共函数的代码 __construct($registry) { $this->registry = $registry; } 在公共函数中 __construct() { $this->errors = array(); }?
  • 使用parent::__construct

标签: php error-handling opencart


【解决方案1】:

错误不在抽象类中。这是您实际通过调用 $var->property1 直接调用 a 属性的地方,因为它显然是产生错误的 __get() 魔术方法,它调用了类 get() 方法。您的控制器的注册表对象需要具有 get() 方法。您可能没有将正确的注册表 obj 传递到控制器构造函数中。

【讨论】:

  • 我更新了我的问题。我在使用 $registry 的页面上添加了代码。你能看看出了什么问题。你知道如何解决它吗?
  • 让我们看看你的注册表类。
猜你喜欢
  • 2015-03-29
  • 1970-01-01
  • 2022-12-21
  • 2021-10-27
  • 2021-01-23
  • 1970-01-01
  • 2020-10-04
  • 2020-01-19
  • 2017-06-18
相关资源
最近更新 更多