Jump to content
iwato

A Nested Class? Or, Inheritance?

Recommended Posts

BACKGROUND:  I am creating a custom class for use with PHPMailer that will hopefully eliminate redundancy in its application and even facilitate my use of the class.  One of my questions deals with inheritance and the other has to do with scope.

My current use of PHPMailer is quite simple.  It consists of a folder containing four files:  three of these are PHP class definitions and one of them is an autoloader.  PHPMailer is called in the following manner:

require_once '../../../PHPMailerAutoload.php';
$mail = new PHPMailer;

Once the instance $mail has been created I have access to a large number of public functions that I have thought to consolidate into a single class of my own making called PHPGlobalMailer.  My construction of this class appears awkward, and I would like to solicit your feedback in an attempt to remove its clumsiness.

class PHPGlobalMailer {
    private $mail;
    private static $char_set = 'UTF-8';
    private static $smtp_debug = 0;
        // SMTP::DEBUG_OFF (0): Disable debugging (default).
        // SMTP::DEBUG_CLIENT (1): Output messages sent by the client.
        // SMTP::DEBUG_SERVER (2): as 1, plus responses received from the server.
        // SMTP::DEBUG_CONNECTION (3): as 2, plus more information about the initial connection (used to diagnose STARTTLS failures.
        // SMTP::DEBUG_LOWLEVEL (4): as 3 (Verbose).
    private static $debug_output = 'html';
        // 'echo': Plain text output (default).
        // 'html': Browser output.
        // 'error_log': Output to php.ini configured error.log.
        // function($str, $level) {echo "Debug Level: " . $level; "Error Message: " .  $str;}

    private static $hostserver = '...';
    private static $server_port = ...;

    private static $smpt_auth = 'true';
    private static $mail_account_name = '...';
    private static $mail_account_password = '...';

    private static $from_mail_address = '...';
    private static $from_mail_name = '...';
    private static $reply_to_address = '...';
    private static $reply_to_name = '...';

    public $email = '';
    public $name = '';
    public $subject = '';
    public $html_message = '';
    public $alt_message = '';

    public function __construct($email, $name, $subject, $html_message, $alt_message) {
        if ($this->mail new PHPMailer) {
            $this->mail->CharSet=self::$char_set;
            $this->mail->isSMPT();
            $this->mail->SMTPDebug = self::$smpt_debug;
            $this->mail->Debugoutput = self::$debug_output;
            $this->mail->Host = self::$hostserver;
            $this->mail->Port = self::$server_port;
            $this->mail->SMPTAuth = self::$smpt_auth;
            $this->mail->Username = self::$mail_account_name;
            $this->mail->Password = self::$mail_account_password;
            $this->mail->setFrom(self::$from_mail_address, self::$from_mail_name);
            $this->mail->addReplyTo(self::$reply_to_address, self::$reply_to_name);
            $this->mail->addAddress($this->email, $this->name);
            $this->mail->Subject = $this->subject;
            $this->mail->msgHTML($html_message);
            $this->mail->altBody = $alt_message;
        } else {
            echo "Error: Failure to create new PHPMailer instance.";
        }
    }
    public function get_character_set() {
        return self::$char_set;
    }
    public function set_character_set($char_set) {
        self::$char_set = $char_set;
    }
    public function get_smtp_debug() {
        return self::$smtp_debug;
    }
    public function set_password($smtp_debug) {
        self::$smtp_debug = $smtp_debug;
    }
    public function get_debug_format() {
        return self::$debug_output;
    }
    public function set_debug_format($debug_output) {
        self::$debug_output = $debug_output;
    }
    public function get_debug_format() {
        return self::$debug_output;
    }
    public function set_debug_format($debug_output) {
        self::$debug_output = $debug_output;
    }
    public function get_mail_instance() {
        return $this->mail;
    }
}

The AWKWARDNESS

1)  In order to make it work I must include PHPMailerAutoload.php file.  Is there someway that I could avoid this, say through inheritance?

2)  Notice that I am creating an instance of PHPMailer inside my class and then using the instance's public functions to modify it.  Is this proper?  If not, what would be a more appropriate way of constructing my class.

3)  I am concerned that adjustments to an instance of PHPGlobalMailer  will affect other instances created in a similar manner.  For example, were I to change the value of $smpt_debug for a particular instance of PHPGlobalMailer would it not effect the value of that same parameter for other instances of PHPGlobalMailer?  In effect, I would like full control of each instance, but still maintain constancy with regard to the $account_name and $account_password and other values across all instances.

Could you please comment on each of three points.

Roddy

 

 

 

 

 

Edited by iwato

Share this post


Link to post
Share on other sites
Quote

1)  In order to make it work I must include PHPMailerAutoload.php file.  Is there someway that I could avoid this, say through inheritance?

You always need to include a class you're going to use, whether you use autoloading for that or not.  You can't use a class that hasn't been defined and included.  Inheritance has nothing to do with that, if you extend another class then you need to have that class defined before you even define your class.  If your class depends on another class then make sure the class is defined in your own class definition file.  Then you only need to include your class, which will include the other class itself.

Quote

2)  Notice that I am creating an instance of PHPMailer inside my class and then using the instance's public functions to modify it.  Is this proper?

I suppose, but I don't see your class adding anything of value which PHPMailer does not already provide.  You're just providing a more verbose way to do the same things.  You're also using self incorrectly, it should be $this.

Quote

In effect, I would like full control of each instance

You already have that.  You don't have to invent anything new in order to be able to control every instance of every class you make.  That problem has already been solved.

Quote

but still maintain constancy with regard to the $account_name and $account_password and other values across all instances.

If you want to make them constants, you can make them constants.  Otherwise, just set the same value when you create each instance and don't change them.

This is normally not what constants are used for, because if you want to change the constant you have to edit the class file and that kind of defeats the point of classes.  Constants are used for things that a class provides for which the value itself isn't important, like the various debug settings.  Actual data that the class is using, like usernames and passwords, should be set like any other property.  If you don't want them to change, then just don't change them.  They won't automatically change if nothing tells them to change.

 

You may be thinking of the factory pattern, where you have a static class which creates another class, and when you do that you can set whatever values you want to set for every instance of that class before returning the new instance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×