gpt4 book ai didi

php - 子类中的构造函数将覆盖父类中的构造函数。因此父构造函数不会运行。 OpenCart

转载 作者:行者123 更新时间:2023-12-03 08:11:25 34 4
gpt4 key购买 nike

我正在将程序从Web导出到OpenCart。我正在尝试登录,但收到此错误消息:

PHP Fatal error: Call to a member function get() on null in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php on line 10


我们发现子类中的构造函数会覆盖父类中的构造函数。因此父构造函数不会运行,并且不会设置this-> registry。
Controller 中的代码为:
<?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;
}
}
}
如果我添加 parent::__construct();到它。它仍然不起作用。我不知道该添加哪一个,所以我都尝试了。
当我像这样向 Controller 添加 parent::__construct();时:
<?php
abstract class Controller {
protected $registry;

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

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

public function __set($key, $value) {
$this->registry->set($key, $value);
}
}
然后我收到此错误消息:

PHP Fatal error: Call to a member function get() on null in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php on line 11


如果我将其添加到这样的代码中:
public function __construct()
{
parent::__construct();
$this->errors = array();
}
然后我收到此错误消息:

PHP Warning: Missing argument 1 for Controller::__construct(), called in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/key2publish/k2p_api_OCwriter.php on line 95 and defined in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php on line 5

PHP Notice: Undefined variable: registry in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php on line 6

PHP Fatal error: Call to a member function get() on null in /home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php on line 10


有谁知道如何解决这一问题?我想听听。
谢谢!

最佳答案

Controller类的构造函数将$registry作为参数。因此,当您调用__construct类的Controller时,您需要像这样调用它:

parent::__construct($registry);

因此,您的 K2P_API_OCWRITER构造函数可以是:
class K2P_API_OCWRITER extends Controller
{
public function __construct($registry)
{
// pass `$registry` to parent `__construct`
parent::__construct($registry);
$this->errors = array();
}
}

实例化 Controller对象仍然是:
$myAPI = new K2P_API_OCWRITER($registry);

而且顺便说一句,无需在 K2P_API_OCWRITER构造函数中编写 parent::__construct();,因为它不扩展任何类,因此它没有父级。

关于php - 子类中的构造函数将覆盖父类中的构造函数。因此父构造函数不会运行。 OpenCart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43494444/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com