dtcpvz8162 2017-04-19 11:27
浏览 45
已采纳

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

I am making a web export from our program to OpenCart. I am trying to log in, but i get this error message:

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

We find out that the constructor in child class overwrites the one in parent. So parent constructor does not run and does not set this->registry.

The code in the controller is:

<?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);
        }
    }

This is The code i made:

    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;
        }
     }
    }

If i add parent::__construct(); to it. It still don't work. I didn't know by which one I had to add it, so i tried both.

When i added parent::__construct(); to the controller like this:

<?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);
    }
}

Then i get this error message:

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

And if i add it to my made code like this:

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

Then i get this error messages:

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

Does anyone know how to fix this? I would like to hear.

Thanks!

  • 写回答

1条回答 默认 最新

  • douchensou6495 2017-04-19 11:38
    关注

    Constructor of your Controller class takes $registry as argument. So when you call __construct of Controller class, you need to call it like:

    parent::__construct($registry);
    

    So, your constructor for K2P_API_OCWRITER which Controller can be:

    class K2P_API_OCWRITER extends Controller
    {
        public function __construct($registry)
        {
            // pass `$registry` to parent `__construct`
            parent::__construct($registry);
            $this->errors = array();
        }
    }
    

    And instantiating an object of K2P_API_OCWRITER is still:

    $myAPI = new K2P_API_OCWRITER($registry);
    

    And btw there's no need to write parent::__construct(); in Controller constructor, as it does not extend any classes, so it does not have parent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!