How to simplify using classes with namespaces

by Andy Prevost

Sunday December 31 2023

Using classes with namespaces can get a bit daunting ... at least confusing ... when the provider has multiple classes in varying categories.

I'll use my own classes as an example, and show how to simplify the coding.

My full list of classes and their namespaces:

PHPMailer Pro namespace: "codeworxtech\PHPMailerPro"
PHPMailer-BMH namespace: "codeworxtech\PHPMailerBMH"
PHPMailer-FE namespace: "codeworxtech\PHPMailerFE"
SMTPMailer namespace: "codeworxtech\SMTPMailer"
FormValidator namespace: "codeworxtech\FormValidator"

Initiating the class means coding structure that is:

require_once 'assets/mailer/PHPMailer.Pro.php';
$mail = new codeworxtech\PHPMailerPro\PHPMailerPro();

That can be a bit confusing, blending the namespace with the class name. One way to deal with that is to alias the namespace/class as in:

require_once 'assets/mailer/PHPMailer.Pro.php';
class_alias('codeworxtech\PHPMailerPro\PHPMailerPro','PHPMailerPro');
$mail = new PHPMailerPro();

 

◀ Previous Next ▶

Post a Comment