drkwpgrdb092239314 2018-09-30 15:04
浏览 64

未捕获错误:从抽象类调用null上的成员函数set()

I'm calling a function from an abstract class that's extending Controller already.

abstract class RestController extends Controller {

    public $appVersion = null;

    public function checkPlugin() {

        $this->config->set('config_error_display', 0);

        $this->response->addHeader('Content-Type: application/json; charset=utf-8');

        if (!$this->config->get('rest_api_status')) {
            $json["error"] = 'API is disabled. Enable it!';
        }

        if(isset($json["error"])){
            header("Content-Type: application/json;charset=utf-8");
            echo(json_encode($json));
            exit;
        }

        $this->validateToken();


        $token = $this->getTokenValue();

        $this->update_session($token['access_token'], json_decode($token['data'], true));

        $headers = apache_request_headers();

        //set language
        $osc_lang = "";
        if(isset($headers['X-Oc-Merchant-Language'])){
            $osc_lang = $headers['X-Oc-Merchant-Language'];
        }else if(isset($headers['X-OC-MERCHANT-LANGUAGE'])){
            $osc_lang = $headers['X-OC-MERCHANT-LANGUAGE'];
        }

        if($osc_lang != ""){
            $this->session->data['language'] = $osc_lang;
            $this->config->set('config_language', $osc_lang);
            $languages = array();
            $this->load->model('localisation/language');
            $all = $this->model_localisation_language->getLanguages();

            foreach ($all as $result) {
                $languages[$result['code']] = $result;
            }
            $this->config->set('config_language_id', $languages[$osc_lang]['language_id']);
        }

        if(isset($headers['X-Oc-Store-Id'])){
            $this->config->set('config_store_id', $headers['X-Oc-Store-Id']);
        } else if(isset($headers['X-OC-STORE-ID'])){
            $this->config->set('config_store_id', $headers['X-OC-STORE-ID']);
        }

        $currency = "";

        if(isset($headers['X-Oc-Currency'])){
            $currency = $headers['X-Oc-Currency'];
        } else if(isset($headers['X-OC-CURRENCY'])){
            $currency = $headers['X-OC-CURRENCY'];
        }

        if (!empty($currency)) {
            $this->currency->set($currency);
        }

        if(isset($headers['X-Oc-Image-Dimension'])){
            $d = $headers['X-Oc-Image-Dimension'];
            $d = explode('x', $d);
            $this->config->set('config_rest_api_image_width', $d[0]);
            $this->config->set('config_rest_api_image_height', $d[1]);
        } else if(isset($headers['X-OC-IMAGE-DIMENSION'])){
            $d = $headers['X-OC-IMAGE-DIMENSION'];
            $d = explode('x', $d);
            $this->config->set('config_rest_api_image_width', $d[0]);
            $this->config->set('config_rest_api_image_height', $d[1]);
        } else {
            $this->config->set('config_rest_api_image_width', 500);
            $this->config->set('config_rest_api_image_height', 500);
        }

        if(defined('VERSION')) {
            $version = str_replace('.', '', VERSION);
            $this->appVersion = sprintf('%-04s', $version);
        } else {
            $this->appVersion = 1234;
        }
    }

Since according to OOP you can't Cannot instantiate abstract class what I did is created a new controller class extending the abstract class.

require_once(DIR_SYSTEM . 'engine/restcontroller.php');

class ControllerRestRestApi extends RestController {

    function __construct(){
        //public $opencartVersion = null;
        parent::__construct('checkPlugin');
        $this->checkPlugin();
    }

    public function __get($json) {
      return $this->checkPlugin($json);
    }

    public function __set($json, $token) {
      $this->checkPlugin($json, $token);
    }
    //
    // public function __call($methodName, $args)
    // {
    //   call_user_func_array(array($this, $methodName), $args);
    //   call_user_func_array(array('parent', $methodName), $args);
    // }

    function parentCheckPlugin(){

      // parent::checkPlugin();
      echo "Say Hello";
    }
}

Then I called the abstract class from the new extended controller class to call the function I wan't from the main abstract class with use of VQMod.

  <operation>
    <search position="before"><![CDATA[
         class ControllerProductCategory extends Controller {
     ]]></search>
     <add><![CDATA[
       require_once(DIR_APPLICATION . 'controller/rest/extrestapi.php');
     ]]></add>
  </operation>

  <operation>
    <search position="after"><![CDATA[
         public function index() {
     ]]></search>
     <add><![CDATA[
      $checkPlugin = new ControllerRestRestApi();
       $checkPlugin->parentCheckPlugin();
     ]]></add>
  </operation>

        <operation>
            <search position="replace"><![CDATA[
             $this->response->setOutput($this->render());
             ]]></search>
            <add><![CDATA[

          if ( $_SERVER['REQUEST_METHOD'] === 'GET' ){
                  $json['success']  = true;
                  $this->response->addHeader('Content-Type: application/json; charset=utf-8');
                  $this->response->setOutput(json_encode($this->data));
              }else {
                  $this->response->setOutput($this->render());
         }

            ]]></add>

    </operation>

Now I'm testing it using Postman after I called the http and added the valid token I got an error.

Notice: Undefined property: ControllerRestRestApi::$config in /home/user/public_html/system/engine/restcontroller.php on line 10
Fatal error: Uncaught Error: Call to a member function set() on null in /home/user/public_html/system/engine/restcontroller.php:10 Stack trace:

0 /home/user/public_html/catalog/controller/rest/extrestapi.php(13): RestController->checkPlugin('config')

1 /home/user/public_html/system/engine/restcontroller.php(10): ControllerRestRestApi->__get('config')

2 /home/user/public_html/catalog/controller/rest/extrestapi.php(9): RestController->checkPlugin()

3 /home/user/public_html/vqmod/vqcache/vq2-catalog_controller_product_category.php(9):

ControllerRestRestApi->__construct()

4 /home/user/public_html/vqmod/vqcache/vq2-system_engine_front.php(47):

ControllerProductCategory->index()

5 /home/user/public_html/vqmod/vqcache/vq2-system_engine_front.php(29):

Front->execute('')

6 /home/user/public_html/index.php(237): Front->dispatch(Object(Action), Object(Action))

7 {main} thrown in /home/user/public_html/system/engine/restcontroller.php on line

10

Acccording to this error it's referring to this line and all after in main abstract class function

public function checkPlugin() {

    $this->config->set('config_error_display', 0);

    $this->response->addHeader('Content-Type: application/json; charset=utf-8');

This same function checkPlugin() works perfectly in other controller as such

require_once(DIR_SYSTEM . 'engine/restcontroller.php');

class ControllerFeedRestApi extends RestController {

    public function getInfo() {

        $this->checkPlugin();

But Not the other controller I'm using and trying to achieve. What did I do wrong and how to resolve it any help please, thanks in advanced!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置