我收到此错误消息:
PHP致命错误:在第10行的/home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php中调用null上的成员函数get()
控制器中的所有代码均为:
<?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;
}
}
}
我该如何解决?
答案 0 :(得分:0)
错误不在抽象类中。这是你通过调用$ var-&gt; property1直接调用a属性的地方,因为它显然是产生错误的__get()魔术方法,它调用类get()方法。您的控制器的注册表对象需要具有get()方法。您可能没有将正确的注册表obj传递给控制器构造函数。