skip to Main Content

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


  1. Wouldn’t you just reference the desired exception by its full name?

    class alpha {
      function alphafunction {
         $a = new PHPMailerExceptionException();
      }
    }
    
    class beta {
      function betafunction {
         $a = new AnotherClassExceptionException();
      }
    }
    
    Login or Signup to reply.
  2. 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:

    namespace MyNamespace
    {
        class MyClass
        {
            // Use the alias "alias" for the namespace "MyNamespace::MyOtherNamespace"
            using alias = MyNamespace::MyOtherNamespace;
    
            void MyFunction()
            {
                // Use the alias to access a class or function in the namespace
                alias::MyClass instance;
                instance.DoSomething();
            }
        }
    }
    

    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.

    Login or Signup to reply.
  3. On one side you can use different files for different classes that use different Exceptions.

    alpha .php

    use PHPMailerPHPMailerException;
    class alpha {
      function alphafunction {
         throw new Exception();
      }
    }
    

    beta.php

    use AnotherClassExceptionException;
    class beta{
      function alphafunction {
         throw new Exception();
      }
    }
    

    But you can also do in one file:

    use PHPMailerPHPMailerException as PHPMailerException ;
    use AnotherClassExceptionException as AnotherClassException;
    
    class alpha {
      function alphafunction {
         throw new PHPMailerException();
      }
    }
    
    class beta {
      function betafunction {
         throw new AnotherClassException();
      }
    }
    
    

    Topic here: namespace aliases

    If you want to know what exception you get from elsewhere do:

    try {
      //do stuff here
    } catch(Exception $e){
      print get_class($e);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search