代码之家  ›  专栏  ›  技术社区  ›  Shasha

致命错误-调用未定义的方法“customersss::throwerror()”

php
  •  2
  • Shasha  · 技术社区  · 8 年前

    我创建了一个类并尝试调用一个方法,但是在 错误日志 ,我得到回应 "Uncaught Error: Call to undefined method Customersss::throwError()" ,请问我做错了什么,因为我知道我创造了 throwError() ,我似乎无法访问该类。

    首先是要打电话给 对象

    $this->throwError(INVALID_DATA_TTT, "Invalid shipping fee"); //WHERE I SUSPECT ERROR IS BEING GENERATED
    

    完全错误

       PHP Fatal error:  Uncaught Error: Call to undefined method
       Customersss::throwError() in
       /home/osconliz/public_html/Osconlizapicall/customers.php:276 Stack
       trace:
       #0 /home/osconliz/public_html/Osconlizapicall/api.php(177): Customersss->insertNewDelivery()
       #1 [internal function]: Api->create_insert_new_delivery()
       #2 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
       #3 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
       #4 {main}   thrown in /home/osconliz/public_html/Osconlizapicall/customers.php on line 276
    


    客户.php

    require_once('constants.php');
    require_once('rest.php');   
    class Customersss {     
    private $id;
    private $shipping_fee;
    private $pickup_fee; 
    
    function setId($id){ $this->id = $id; }
    function getId() { return $this->id; }
    
    function setShipping_fee($shipping_fee){ $this->shipping_fee = $shipping_fee; }
    function getShipping_fee() { return $this->shipping_fee; }
    
    function setPickup_fee($pickup_fee){ $this->pickup_fee = $pickup_fee; }
    function getPickup_fee() { return $this->pickup_fee; }
    
    public function __construct(){
    $db = new DbConnect();
    $this->dbConn = $db->connect();
    }
    
    public function insertNewDelivery(){
    if ($this->shipping_fee == ""){
    $this->throwError(EMPTY_PARAMETER, "Empty shipping fee");
    exit();
    }   
    
    if ($this->shipping_fee == ""){
    $this->throwError(INVALID_DATA_TTT, "Invalid shipping fee");
    exit();
    }   
    }
    
    
    }
    


    REST PHP

     require_once('constants.php');
        class Rest {
        protected $request;
        protected $serviceName;
        protected $param;
    
          public function processApi(){
          $api = new API;
          $rMethod = new reflectionMethod('API', $this->serviceName);
          if(!method_exists($api, $this->serviceName)){
          $this->throwError(API_DOST_NOT_EXIST, "API does not exist");
           }
            $rMethod->invoke($api);
        }
    
        public function throwError($code, $message){
             header("content-type: application/json");
             $errorMsg = json_encode(['error' => ['status'=>$code, 'message'=>$message]]);
             echo $errorMsg; exit;       
        }
        }
    


    常量.php

     define('INVALID_DATA_TTT',          350);
         define('EMPTY_PARAMETER',           404);
         define('API_DOST_NOT_EXIST',        400);
         define('ACCESS_TOKEN_ERRORS',       500);
    


    API.PHP

    require_once "customers.php";
    require_once "constants.php";
    
    class Api extends Rest {
    public $dbConn;  
    
    public function __construct(){
    parent::__construct();  
    $db = new DbConnect;
    $this->dbConn = $db->connect();
    
    }
      public function create_insert_new_delivery(){
      $shipping_fee= $this->validateParameter('item_category', $this->param['shipping_fee'], STRING, true);
    
    
      try {
     $cust = new Customersss;
     }  catch (Exception $e){
     $this->throwError(ACCESS_TOKEN_ERRORS, $e->getMessage());
    
    }
    }
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   AymDev    8 年前

    你在打电话 Rest::ThrowError() Customersss 班级。这意味着在此上下文中无法访问您的函数。
    调用此 Rest 方法来自 客户服务系统 同学们,你们可以:

    • 延伸 休息 客户服务系统 (见 inheritance )
    • 制作 ThrowError() 静态方法(参见 static )

    使用继承:

    class Customersss extends Rest { /* your properties/methods */ }
    

    使用静态方法:

    class Rest {
        static public function throwError($code, $message){ /* your code */ }
    }
    

    可调用 rest::throwerror()

    推荐文章