PHP Trait 覆盖 protected Trait 方法

标签 php laravel laravel-5 traits laravel-cashier

我正在处理的 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/

相关文章:

PHP 从用户输入中删除 html 和 php 代码

mysql - PHP-FPM 数据库中的日志错误

laravel - 如何在 Laravel 上的电子邮件布局中显示图像?

php - Laravel:将大量任务放入队列中

laravel - 导入xls文件时加号被删除

laravel-5 - 有什么方法可以在laravel 5中发送闭包?

php - 将 PHP 变量从 while 循环传递到 javascript 函数

laravel - 将 StdClass 数据移植到模型

mysql - Laravel 5.1 DBQuery 连接

PHP foreach 循环仅每隔一次迭代递增一次