In my script, I have two classes with the name "Exception".
Normally, I would use
use PHPMailerPHPMailerException;
and then, use the class Exception that points to PHPMailer.
The problem is that I can’t use the "use" command inside a function. So how can I declare what exception to use each time?
Example:
use PHPMailerPHPMailerPHPMailer;
use PHPMAilerPHPMailerExceptions;
use PHPMailerPHPMailerSMTP;
class email {
function send_with_smtp {
$a = new PHPMailer(); // inside this class it needs the exceptions to be used as a PHPMailerExceptions
}
function send_with_critsend{
$a = new MXM(); // inside this class it needs the exceptions to be used NOT as a PHPMailerExceptions
}
}
What I would do -but can’t because use is used only in the upper level- is the following (that is wrong of course). But this is what I would like to achieve:
class email {
function send_with_smtp {
use PHPMailerPHPMailerPHPMailer;
use PHPMAilerPHPMailerExceptions;
use PHPMailerPHPMailerSMTP;
$a = new PHPMailer(); // inside this class it needs the exceptions to be used as a PHPMailerExceptions
}
function send_with_critsend{
use ANOTHER; // maybe the default Exceptions or something else.
$a = new MXM(); // inside this class it needs the exceptions to be used NOT as a PHPMailerExceptions
}
}
3
Answers
Wouldn’t you just reference the desired exception by its full name?
To use a namespace alias (also called a "using directive") inside a class or function, you can place the using keyword followed by the namespace and the alias you want to use at the top of your class or function definition. For example:
In this example, the using directive creates an alias for the MyOtherNamespace namespace inside the MyNamespace namespace. This allows you to use the alias to access classes or functions in the MyOtherNamespace namespace without having to fully qualify their names.
On one side you can use different files for different classes that use different Exceptions.
alpha .php
beta.php
But you can also do in one file:
Topic here: namespace aliases
If you want to know what exception you get from elsewhere do: