PHP Trait Override Protected Trait Method

青春壹個敷衍的年華 提交于 2019-12-11 06:35:32

问题


I am having problems with a composer package I am dealing with. It implements a trait 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,
                ]);
            }
        }
    }
}

I am trying to override this method in my class

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,
                ]);
            }
        }
    }
}

But the method is not overridden. As a test I overrode some of the public functions and they work fine, it this a limitation of traits? I have tried to find the answer online but have come up short.

I am trying to override this function because I need to customize the behaviour of the BraintreeSubscription class.

Any help would be greatly appreciated.


回答1:


in your class you could do the following notice the T before the function name you may change this to be aliased as anything really.

use billable {
    updateSubscriptionsToPaymentMethod as tUpdateSubscriptionsToPaymentMethod;
}

then simply in the class add the desired function:

    public function updateSubscriptionsToPaymentMethod(){
      ...
   }


来源:https://stackoverflow.com/questions/39441612/php-trait-override-protected-trait-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!