Recap - Payment Processor
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 37 of 91.
Challenge
EasyLet's build a complete payment processing system that brings together everything you've learned about polymorphism. You'll create an interface-based architecture where different payment methods can be processed through a single, unified processor — demonstrating how polymorphism makes code flexible and extensible.
You'll organize your code across five files:
PaymentMethod.php— Define aPaymentMethodinterface with two method signatures:process(float $amount): stringfor handling the payment, and a static methodgetMethodName(): stringthat returns the payment type name. This contract ensures any payment method can be processed uniformly.CreditCard.php— Create aCreditCardclass that implementsPaymentMethod. Include the interface file. The class should have a private$lastFourDigitsproperty set through the constructor. Implementprocess()to return"Processing $[amount] via Credit Card ending in [lastFourDigits]"(format amount to 2 decimal places). Implement the staticgetMethodName()using late static binding to return"Credit Card".PayPal.php— Create aPayPalclass that implementsPaymentMethod. Include the interface file. The class should have a private$emailproperty set through the constructor. Implementprocess()to return"Processing $[amount] via PayPal account [email]"(format amount to 2 decimal places). Implement the staticgetMethodName()to return"PayPal".PaymentProcessor.php— Create aPaymentProcessorclass that orchestrates payments. Include the interface file. Add a methodexecutePayment(PaymentMethod $method, float $amount): stringthat uses interface type hinting to accept any payment method. This method should return the result of callingprocess()on the payment method. Add another methoddescribeMethod(PaymentMethod $method): stringthat returns"Payment via: [methodName]"using the staticgetMethodName()from the passed payment method's class.main.php— Include all the payment class files and the processor. Create aPaymentProcessorinstance. You'll receive three inputs: the last four digits of a credit card, a PayPal email address, and a payment amount. Create both aCreditCardand aPayPalinstance with their respective identifiers. Convert the amount to a float. Use the processor to execute a payment with the credit card first, then with PayPal. Print each result on its own line. Finally, print the method description for the PayPal payment method.
The beauty of this design is that your PaymentProcessor doesn't know anything about credit cards or PayPal specifically — it only knows about the PaymentMethod interface. You could add a BankTransfer or Cryptocurrency class tomorrow, and the processor would handle them without any modifications. That's the power of interface-based polymorphism combined with late static binding!
Try it yourself
<?php
// Include all required files
require_once 'CreditCard.php';
require_once 'PayPal.php';
require_once 'PaymentProcessor.php';
// Read inputs
$lastFourDigits = trim(fgets(STDIN));
$paypalEmail = trim(fgets(STDIN));
$amount = floatval(trim(fgets(STDIN)));
// TODO: Create a PaymentProcessor instance
// TODO: Create a CreditCard instance with the last four digits
// TODO: Create a PayPal instance with the email
// TODO: Execute payment with credit card and print the result
// TODO: Execute payment with PayPal and print the result
// TODO: Print the method description for the PayPal payment method
?>All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern