我想具有一个通用功能,用于从条带中检索对象(客户,订阅,优惠券等)。
我可能并没有采用正确的方法,但是在处理每种类型的检索错误时,该功能变得相当长-我正在反复尝试条纹文档提供的try catch错误处理示例。开关。所以我只想有一个try catch语句,并让try根据我要检索的对象类型动态评估。这可能吗?否则,我可以使用所有try catch语句返回非常长的函数。
function stripe_retrieve_object($objectName,$objectId) {
switch ($objectName) {
case "coupon":
$retrieveStripeCode = "\Stripe\Coupon::retrieve(\"$objectId\");";
break;
case "customer":
$retrieveStripeCode = "\Stripe\Customer::retrieve(\"$objectId\");";
break;
case "subscription":
$retrieveStripeCode = "\Stripe\Subscription::retrieve(\"$objectId\");";
break;
default:
echo "There was a generic error. We were unable to retrieve payment data, and no updates to payment were made. Please contact admin@email.com";
} # switch ($objectName) {
try {
$stripeTry = eval($retrieveStripeCode);
return $stripeTry;
catch(\Stripe\Error\Card $e) { ... }
} # function stripe_retrieve_object($objectName,$objectId)
我在Stripe的日志中看到正在进行调用以检索对象,但是响应主体在Stripe仪表板上为空,并且$ stripeTry = eval($ retrieveStripeCode)没有任何返回值;我尝试了var_dump($ stripeTry),结果返回NULL。
答案 0 :(得分:1)
您可以使用variable functions代替eval()
。
请注意,使用'ClassName::methodName'
作为变量函数需要PHP 7.x。
function stripe_retrieve_object($objectName,$objectId) {
$
switch ($objectName) {
case "coupon":
$retrieveStripeCode = '\Stripe\Coupon::retrieve';
break;
case "customer":
$retrieveStripeCode = '\Stripe\Customer::retrieve';
break;
case "subscription":
$retrieveStripeCode = '\Stripe\Subscription::retrieve';
break;
default:
echo "There was a generic error. We were unable to retrieve payment data, and no updates to payment were made. Please contact admin@email.com";
} # switch ($objectName) {
try {
$stripeTry = $retrieveStripeCode($objectId);
return $stripeTry;
catch(\Stripe\Error\Card $e) { ... }
}