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:
-
Dependency Injection:
- When you create an instance of the
Invoiceclass, you pass an object that implements theBillingGatewayinterface to its constructor. For example:$paypalGateway = new PaypalGateway(); $invoice = new Invoice($paypalGateway); - In this case,
$paypalGatewayis an instance of thePaypalGatewayclass, which implements theBillingGatewayinterface.
- When you create an instance of the
-
Constructor Assignment:
- Inside the
Invoiceclass constructor, the passedBillingGatewayobject is assigned to the$gatewayproperty:public function __construct(BillingGateway $gateway) { $this->gateway = $gateway; } - So,
$this->gatewaynow refers to the$paypalGatewayobject.
- Inside the
-
Method Call:
- When you call
$this->gateway->charge($amount);in thepaymethod of theInvoiceclass, it is equivalent to calling$paypalGateway->charge($amount);because$this->gatewayis the$paypalGatewayobject. - The
chargemethod of thePaypalGatewayclass will be executed with the$amountparameter.
- When you call
Example Walkthrough:
<?php
interface BillingGateway
{
public function charge($amount);
}
class PaypalGateway implements BillingGateway
{
public function charge($amount)
{
echo "Charging $amount using PayPal.\n";
}
}
class Invoice
{
private $gateway;
public function __construct(BillingGateway $gateway)
{
$this->gateway = $gateway;
}
public function pay($amount)
{
$this->gateway->charge($amount);
}
}
// Create a PaypalGateway instance
$paypalGateway = new PaypalGateway();
// Inject the PaypalGateway into the Invoice
$invoice = new Invoice($paypalGateway);
// Pay using the Invoice, which will use the PaypalGateway to charge
$invoice->pay(100);
?>
Output:
Charging 100 using PayPal.
Explanation:
- The
PaypalGatewayinstance is created and passed to theInvoiceconstructor. - The
Invoiceclass stores this instance in its$gatewayproperty. - When the
paymethod of theInvoiceclass is called with an amount, it calls thechargemethod on thePaypalGatewayinstance stored in$this->gateway. - The
chargemethod of thePaypalGatewayclass 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 liked a comment+100 XP
5mos ago
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 wrote a reply+100 XP
5mos ago
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 liked a comment+100 XP
5mos ago
@tiendungdev I highly recommend you to follow these items to learn and understand better:
tiendungdev liked a comment+100 XP
5mos ago