子类中的构造函数会覆盖父类中的构造函数。所以父构造函数不会运行。 Opencart的

时间:2017-04-19 11:27:24

标签: php error-handling opencart

我正在从我们的程序向OpenCart进行Web导出。我正在尝试登录,但是我收到此错误消息:

  

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

我们发现子类中的构造函数会覆盖父类中的构造函数。因此父构造函数不会运行,也不会设置this->注册表。

控制器中的代码是:

<?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();。它仍然无法正常工作。我不知道我必须添加哪一个,所以我尝试了两个。

当我将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致命错误:在第11行的/home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php中调用null上的成员函数get()

如果我将它添加到我的代码中,就像这样:

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

然后我收到此错误消息:

  

PHP警告:缺少Controller :: __ construct()的参数1,在第95行的/home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/key2publish/k2p_api_OCwriter.php中调用,并在/ home /中定义第5行的key2demo / domains / key2datafeed.com / public_html / ocdemoshops / oc23 / system / engine / controller.php

     

PHP注意:未定义的变量:第6行/home/key2demo/domains/key2datafeed.com/public_html/ocdemoshops/oc23/system/engine/controller.php中的注册表

     

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

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

谢谢!

1 个答案:

答案 0 :(得分:1)

@NgModule({ imports: [ CommonModule, FormsModule, RouterModule ], declarations: [Layout, Sidebar, Navbar], providers: [provideRoutes(PagesRoutes)] }) export class LayoutModule { } 类的构造函数以Controller为参数。因此,当您拨打$registry班级的__construct时,您需要将其称为:

Controller

那么,parent::__construct($registry); 的构造函数K2P_API_OCWRITER可以是:

Controller

实例化class K2P_API_OCWRITER extends Controller { public function __construct($registry) { // pass `$registry` to parent `__construct` parent::__construct($registry); $this->errors = array(); } } 的对象仍然是:

K2P_API_OCWRITER

顺便说一下,没有必要在$myAPI = new K2P_API_OCWRITER($registry); 构造函数中编写parent::__construct();,因为它没有扩展任何类,所以它没有父类。