0

I'm working with Laravel 4.2, calling a payment flow between form submit and the controller response. If the payment is accepted, a bunch of work is done behind the scene by the class PaymentProcessor

use MyProject\libraries\payment\PaymentProcessor;

class MyFirstController extends \Controller {

    protected $paymentProcessor;

    public function __construct(
        PaymentProcessor $paymentProcessor
    ) {
        $this->paymentProcessor = $paymentProcessor;
    }

    public function postFormSubmit() {
        //DO SOME STUFF
        $paymentResult = $this->paymentProcessor->makePayment($paymentDetails);
    }
}

PaymentProcessor is on a different namespace, and I'm able to call a needed library using App::make

<?php namespace MyProject\libraries\payment;

use MyProject\DataObjects\PaymentDetails;

class PaymentProcessor {

    public function makePayment(PaymentDetails $paymentData) {
        $doFirstStep = \App::make('amazingLibrary')->doImportantThings();

but, for testing purposes I want to remove all instantiations and calls to other classes directly from PaymentProcessor, so I've tried to do the following injection:

<?php namespace MyProject\libraries\payment;

use MyProject\DataObjects\PaymentDetails;

class PaymentProcessor {
    private $app;

    public function __construct(\App $app) {
        $this->app = $app;
    }

And tried:

    public function makePayment(PaymentDetails $paymentData) {
        $doFirstStep = $this->app::make('amazingLibrary')->doImportantThings();

But it leads to:

FatalErrorException (E_PARSE) syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

Am I in the right way?

Update:

I've also tried to call it as: $this->app->make

That leads to:

Call to undefined method Illuminate\Support\Facades\App::make()

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
André Teixeira
  • 2,392
  • 4
  • 28
  • 41
  • Comment out private app property. I want to see the next result – Canaan Etai Sep 23 '14 at 07:00
  • The result is the same – André Teixeira Sep 23 '14 at 07:03
  • `$this->app::make()` is invalid code. When using `::` you have to qualify it with the name of the class that the method belongs to (e.g. `SomeClass::make()`. – Sverri M. Olsen Sep 23 '14 at 07:03
  • This is a bad idea - you are trying to inject your whole application into your tiny module - think about, read about `SOLID`, spe `Dependency Injection` and redesign. For the `::` - right way is `$this->app->make` since you already instantiated this object, and `::` is namespace (scope) resolution operator, so you use it to indicate, what (static) method / property you'd like to use. – Jarek Tkaczyk Sep 23 '14 at 07:10
  • I don't see as duplicate since the referred question is not talking about my injection doubts. T_PAAMAYIM_NEKUDOTAYIM is only one of the 2 errors I've exposed, and the reffered question shows how to call static methods but didn't solve my problem. @Marcin Nabiałek perceived it and came with a very objective answer, different of all 7 from the other question, and solved my problem. – André Teixeira Sep 23 '14 at 15:55
  • 1
    @AndréTeixeira That's probably because of your question title. I've changed it and nominated this question for reopen – Marcin Nabiałek Sep 23 '14 at 16:06

1 Answers1

2

Probably you want to do something like that:

something($app);

function something (\Illuminate\Foundation\Application  $app) {

    echo  $app->getLocale();
}

So in your case you need to use $this->app->make syntax and you need to pass parameter as I showed (and $app is instance of \Illuminate\Foundation\Application not \App)

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291