tiendungdev's avatar

tiendungdev liked a comment+100 XP

1w ago

This is what made it click for me:

So $this->gateway->charge($amount); in the invoice class could be interpreted as - in case of a PaypalGateway object - as PaypalGateway ->charge(amount)?

Yes, that's correct! The line $this->gateway->charge($amount); in the Invoice class is a call to the charge method of the object that was injected into the Invoice class via its constructor.

Let's break it down step by step:

  1. Dependency Injection:

    • When you create an instance of the Invoice class, you pass an object that implements the BillingGateway interface to its constructor. For example:
      $paypalGateway = new PaypalGateway();
      $invoice = new Invoice($paypalGateway);
      
    • In this case, $paypalGateway is an instance of the PaypalGateway class, which implements the BillingGateway interface.
  2. Constructor Assignment:

    • Inside the Invoice class constructor, the passed BillingGateway object is assigned to the $gateway property:
      public function __construct(BillingGateway $gateway)
      {
          $this->gateway = $gateway;
      }
      
    • So, $this->gateway now refers to the $paypalGateway object.
  3. Method Call:

    • When you call $this->gateway->charge($amount); in the pay method of the Invoice class, it is equivalent to calling $paypalGateway->charge($amount); because $this->gateway is the $paypalGateway object.
    • The charge method of the PaypalGateway class will be executed with the $amount parameter.

Example Walkthrough:

Output:

Charging 100 using PayPal.

Explanation:

  • The PaypalGateway instance is created and passed to the Invoice constructor.
  • The Invoice class stores this instance in its $gateway property.
  • When the pay method of the Invoice class is called with an amount, it calls the charge method on the PaypalGateway instance stored in $this->gateway.
  • The charge method of the PaypalGateway class is executed, printing the message "Charging 100 using PayPal."

This demonstrates how dependency injection allows the Invoice class to use any implementation of the BillingGateway interface without being tightly coupled to a specific implementation.

tiendungdev's avatar

tiendungdev liked a comment+100 XP

5mos ago

You can subscribe here on Laracast and you will have access to all series with great teachers.

tiendungdev's avatar

tiendungdev liked a comment+100 XP

5mos ago

To add to @vincent15000 answer, a lot of the courses here also has the complete source code on Github.

But I suggest following the video and actually code as you learn, meaning you type it, not copy and paste. Understand the why and how of it.

Edit: While learning I would suggest no AI help at first.

tiendungdev's avatar

tiendungdev wrote a reply+100 XP

5mos ago

Thanks everyone. I'm looking for a suitable learning method.

tiendungdev's avatar

tiendungdev liked a comment+100 XP

5mos ago

@tiendungdev “Buying” source code isn’t going to give you the most vital ingredient when it comes to writing code: the why and the how. All you’re seeing is the end result. You‘re not seeing the decisions that went in to arriving at that solutions, the alternatives considered and disregarded, and the pros and cons that were considered between all possible options.

If you want to get better at coding, write more code. Coding is a skill, and like any other skill you get better at it by practicing. You can’t “buy” your way to knowledge.

tiendungdev's avatar

tiendungdev liked a comment+100 XP

5mos ago

tiendungdev's avatar

tiendungdev liked a comment+100 XP

5mos ago

There are a lot of open source projects on Github that has the code freely available.

You can start with packagist.org and search for available packages.