为了访问我的数据库,我创建了一个单例对象,并像访问它一样db::db->query($sql);
好!
class db {
private static $instance = NULL;
private function __construct() {} //Make private
private function __clone(){} //Make private
public static function db() //Get instance of DB
{
if (!self::$instance)
{
self::$instance = new PDO("mysql:host=xxx;dbname=xxx", 'xxx', 'xxx');
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
}
我可以并且应该对 PHPMailer 采取同样的方法吗?除了设置导致“尝试访问私有属性”错误的密件抄送属性外,它似乎可以工作。
class email{
public $bcc;
private static $instance = NULL;
private function __construct() {} //Make private
private function __clone(){} //Make private
public static function email() //Get instance of email
{
if (!self::$instance)
{
require_once ('PHPMailer_v5.1/class.phpmailer.php');
self::$instance = new PHPMailer();
self::$instance->IsSMTP();
self::$instance->SMTPAuth = true;
self::$instance->SMTPSecure = "tls"; //Or SSL?
self::$instance->Host = "smtp.gmail.com";
self::$instance->Port = 587; //Maybe 465 instead? SSL only?
self::$instance->Username = "xxx";
self::$instance->Password = "xxx";
}
return self::$instance;
}
}
$mail = email::email();
$mail->AddReplyTo('email@email.com','name');
$mail->SetFrom('email@email.com','name');
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->bcc = 'doesntwork@gmail.com';
$mail->FromName = $users_name;
$mail->Subject = $subject;
$mail->WordWrap = 50; // set word wrap