donglu9896 2018-06-18 13:49
浏览 67
已采纳

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

I created a class and try to call in a method, but however in my error log, I am getting response "Uncaught Error: Call to undefined method Customersss::throwError()", please what am I doing wrong, because I know I have created throwError(), I can't seem to be able to access the class.

Firstly the class trying to call object

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

The full error

   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


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


constants.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条回答 默认 最新

  • duancheng3042 2018-06-18 14:05
    关注

    You are calling Rest::ThrowError() from Customersss class. It means your function is unreachable in this context.
    To call this Rest method from the Customersss class, you can:

    • extend Rest to Customersss (see inheritance)
    • make ThrowError() a static method (see static)

    Using inheritance:

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

    Using a static method:

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

    Callable with Rest::ThrowError()

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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制