What is Composer?
Practice question · write your own answer- Define the concept in one or two sentences.
- Give a small example and trace what happens.
- Explain one limitation, trade-off, or common mistake.
Understand the idea. Compare it. Explain it. Test yourself.
class OrderController
{
public function createOrder()
{
$payment = new PaymentService();
$payment->process();
}
}
class OrderController
{
public function __construct(private PaymentService $paymentService) {}
public function createOrder()
{
$this->paymentService->process();
}
}
interface PaymentGateway
{
public function pay(float $amount);
}
class RazorpayGateway implements PaymentGateway
{
public function pay(float $amount)
{
// Razorpay implementation
}
}
// In a service provider's register():
$this->app->bind(PaymentGateway::class, RazorpayGateway::class);
class PaymentService
{
public function __construct(private PaymentGateway $gateway) {}
}
class PaymentService
{
public function process()
{
//
}
}
class OrderController extends Controller
{
public function __construct(private PaymentService $paymentService) {}
}
class OrderController
{
public function createOrder()
{
$payment = new PaymentService();
$payment->process();
}
}
class OrderController
{
public function __construct(private PaymentService $paymentService) {}
public function createOrder()
{
$this->paymentService->process();
}
}
interface PaymentGateway
{
public function pay(float $amount);
}
class RazorpayGateway implements PaymentGateway
{
public function pay(float $amount)
{
// Razorpay implementation
}
}
// In a service provider's register():
$this->app->bind(PaymentGateway::class, RazorpayGateway::class);
class PaymentService
{
public function __construct(private PaymentGateway $gateway) {}
}
class OrderController
{
public function createOrder()
{
$payment = new PaymentService();
$payment->process();
}
}
class OrderController
{
public function __construct(private PaymentService $paymentService) {}
public function createOrder()
{
$this->paymentService->process();
}
}
interface PaymentGateway
{
public function pay(float $amount);
}
class RazorpayGateway implements PaymentGateway
{
public function pay(float $amount)
{
// Razorpay implementation
}
}
// In a service provider's register():
$this->app->bind(PaymentGateway::class, RazorpayGateway::class);
class PaymentService
{
public function __construct(private PaymentGateway $gateway) {}
}