gpt4 book ai didi

php - fatal error - 调用未定义的方法 "Customersss::throwError()"

转载 作者:行者123 更新时间:2023-12-04 01:55:32 25 4
gpt4 key购买 nike

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

首先类试图调用对象

$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();
}
}


}



休息.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());

}
}

最佳答案

您调用Rest::ThrowError()来自 Customersss类(class)。这意味着您的功能在这种情况下无法访问。
调用此电话Rest来自 Customersss 的方法类,你可以:

  • 延长 RestCustomersss (见 inheritance )
  • 制作 ThrowError()静态方法(见 static)

  • 使用继承:
    class Customersss extends Rest { /* your properties/methods */ }

    使用静态方法:
    class Rest {
    static public function throwError($code, $message){ /* your code */ }
    }

    可调用 Rest::ThrowError()

    关于php - fatal error - 调用未定义的方法 "Customersss::throwError()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50911033/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com