我正在处理的 Composer 包出现问题。它实现了 Billable 特征。
trait Billable
{
/**
* Update the payment method token for all of the user's subscriptions.
*
* @param string $token
* @return void
*/
protected function updateSubscriptionsToPaymentMethod($token)
{
foreach ($this->subscriptions as $subscription) {
if ($subscription->active()) {
BraintreeSubscription::update($subscription->braintree_id, [
'paymentMethodToken' => $token,
]);
}
}
}
}
我正在尝试在我的类中重写此方法
class Organisation extends Model
{
use Billable;
/**
* Update the payment method token for all of the user's subscriptions.
*
* @param string $token
* @return void
*/
protected function updateSubscriptionsToPaymentMethod($token)
{
foreach ($this->subscriptions as $subscription) {
if ($subscription->active()) {
BrntreeSubscription::update($subscription->braintree_id, [
'paymentMethodToken' => $token,
]);
}
}
}
}
但是该方法没有被重写。作为测试,我覆盖了一些公共(public)函数,它们工作正常,这是特征的限制吗?我试图在网上找到答案,但没有找到答案。
我尝试重写此函数,因为我需要自定义 BraintreeSubscription 类的行为。
任何帮助将不胜感激。
最佳答案
在你的类中,你可以执行以下操作,注意函数名称之前的 T,你可以将其更改为任何别名。
use billable {
updateSubscriptionsToPaymentMethod as tUpdateSubscriptionsToPaymentMethod;
}
然后只需在类中添加所需的函数:
public function updateSubscriptionsToPaymentMethod(){
...
}
关于PHP Trait 覆盖 protected Trait 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441612/