1

I've get the following class

<?php

namespace App;


class Currency extends \Casinelli\Currency\Currency
{

    /**
     * Create a new instance.
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct($app)
    {
        parent::__construct($app);
        $this->setCurrency(getCurrency());
    }
}

I've replaced the 'aliases' in app.php:

   - 'Currency' => \Casinelli\Currency\Facades\Currency::class,
   + 'Currency' => \App\Currency::class,

However, I'm running into an error of:

Non-static method Casinelli\Currency\Currency::rounded() should not be called statically

It seems my Currency class is not being treated as a Facade... How would I go about resolving this?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286

2 Answers2

0

Before answering, I recommend to fork the package, do your modifications and use your fork. Because if something change in the package, your override may not work anymore.

Let's take a look at the package.

You have 3 important files :

The currency that you want to extend: https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Currency.php

The facade that you want to use : https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Facades/Currency.php

And finally, the ServiceProvider that register the class you want to extends: https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/CurrencyServiceProvider.php#L60

The service provider will register the class Currency as a singleton with the alias of currency

Then, when you call the facade Currency, it will look for the alias currency and return a the instance of the class Currency.

Implement your own Currency

To use your own Currency class, you will need to register your own implementation of the Currency class in a service provider that will replace the service provider of the package.

  1. Create your own serviceProvider
    $ php artisan make:provider ExtendedCurrencyServiceProvider

  2. In your file app/config/app.php,
    Replace Casinelli\Currency\CurrencyServiceProvider::class,
    with App\Providers\ExtendedCurrencyServiceProvider::class,

  3. In your new service provider change to this
<?php

namespace App\Providers;

use Casinelli\Currency\CurrencyServiceProvider;

class ExtendedCurrencyServiceProvider extends CurrencyServiceProvider
{
     /**
     * Register currency provider.
     */
    public function registerCurrency()
    {
        $this->app->singleton('currency', function ($app) {
            return new App\Yournamespace\CurrencyClass($app);
        });
    }
}
  1. Laravel 5.5+ In your composer.json remove the service provider from the autodiscovery
"extra": {
    "laravel": {
        "dont-discover": [
            "Casinelli\\Currency\\CurrencyServiceProvider"
        ]
    }
},

Now, when you will call \Currency::rounded() it will call your own implementation of the currency.

You don't need to change the Facade.

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
  • Apologies I forgot to mention I already added a "CurrencyServiceProvider", as described in your answer. It still runs into that same error. It does not seem to be executing the lamda function within `registerCurrency`. – Chris Stryczynski Sep 26 '18 at 14:17
  • But you're using the `Facade` as follow : `'Currency' => \Casinelli\Currency\Facades\Currency::class,` and your `CurrencyServiceProvider` register correctly your Currency `Class` as I demonstrated, right ? – Clément Baconnier Sep 26 '18 at 14:34
0

The error message / exception is very misleading...

The issue was due to calling App\Currency::rounded instead of: Casinelli\Currency\Facades\Currency::rounded...

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286