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

致命错误 - 调用未定义的方法“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()

    点赞 打赏 评论

相关推荐 更多相似问题