如何将变量值从包含的文件传递到 class 属性
How to pass variable values from an included file to class properties
我遇到一个问题,当我使用模板函数中的值时,发件人姓名、主题和发件人电子邮件地址为空,但如果我直接在邮件函数中写入电子邮件和主题,它们就会出现。
编辑
我想做什么
1。创建可以根据指定要求的类型进行切换的电子邮件模板。
$type = 'account_verify';
$email = new email($type);
$email->send('user@domain.com');
2。有一种方法可以根据 $type 切换这些模板。
我希望在每个模板中指定所有参数,例如 "subject"、"from_email"、"sender_name"...,以便我只调用 $email->send('user_email' ) 方法仅使用用户电子邮件,其余参数应在指定模板中找到。
参见下面的模板方法。
好的,这是每个模板的样子。
<?php
$token = self::token();
$user = self::user();
$today = date("M j - Y, g:i a");
$verify_link = ""
$from_email = 'department@domain.com';
$sender_name = 'department Name';
$department_name = 'department';
$subject = 'Verify your account';
ob_start();
?>
<!DOCTYPE html>
<html>
..........
<?php
?>
其余如下
public $senderName;
public $from_email;
public $dpt;
public $type;
public $subject;
private function template(){
include_once 'templates/accounts/account_verify.php';
//// the following variable like, "$from_email", are set inside the
///// included template. Every template should have its own
/////// parameters set.
$this->from_email = $from_email;
$this->senderName = $sender_name;
$this->dpt = $department_name;
$this->subject = $subject;
return ob_get_clean();
}
以及发送邮件的函数
public function send($to_email ,$color_scheme = NULL){
var_dump('SUBJECT : '.$this->subject);
///// here if I dump the $this->subject i can see it,
//// but the subject is empty on the email.
$subject = $this->from_email;
$from = "".$subject."<user@domain.com>";
$headers = "From:" . $from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to_email,$this->subject,self::template(),$headers);
}
你的问题的关键是面向对象编程。将包含文件结构化为 class,您可以在包含它后立即对其进行初始化。然后,您将能够直接访问您尝试获取的所有属性(如果它们是 public),或者通过一些获取方法。然后,您还可以通过将模板 html 放入另一个 get 方法来取消输出缓冲。像...
return $templateObj->getHTML();
使每个模板中的 class 相同,或者以类型命名它以简化您的 class 初始化。
$templateObj = new Template();
//or
$templateObj = new $YourTemplateName();
//where $YourTemplateName == "Account_Verify" or whichever template you're loading
我遇到一个问题,当我使用模板函数中的值时,发件人姓名、主题和发件人电子邮件地址为空,但如果我直接在邮件函数中写入电子邮件和主题,它们就会出现。
编辑
我想做什么
1。创建可以根据指定要求的类型进行切换的电子邮件模板。
$type = 'account_verify';
$email = new email($type);
$email->send('user@domain.com');
2。有一种方法可以根据 $type 切换这些模板。 我希望在每个模板中指定所有参数,例如 "subject"、"from_email"、"sender_name"...,以便我只调用 $email->send('user_email' ) 方法仅使用用户电子邮件,其余参数应在指定模板中找到。
参见下面的模板方法。
好的,这是每个模板的样子。
<?php
$token = self::token();
$user = self::user();
$today = date("M j - Y, g:i a");
$verify_link = ""
$from_email = 'department@domain.com';
$sender_name = 'department Name';
$department_name = 'department';
$subject = 'Verify your account';
ob_start();
?>
<!DOCTYPE html>
<html>
..........
<?php
?>
其余如下
public $senderName;
public $from_email;
public $dpt;
public $type;
public $subject;
private function template(){
include_once 'templates/accounts/account_verify.php';
//// the following variable like, "$from_email", are set inside the
///// included template. Every template should have its own
/////// parameters set.
$this->from_email = $from_email;
$this->senderName = $sender_name;
$this->dpt = $department_name;
$this->subject = $subject;
return ob_get_clean();
}
以及发送邮件的函数
public function send($to_email ,$color_scheme = NULL){
var_dump('SUBJECT : '.$this->subject);
///// here if I dump the $this->subject i can see it,
//// but the subject is empty on the email.
$subject = $this->from_email;
$from = "".$subject."<user@domain.com>";
$headers = "From:" . $from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to_email,$this->subject,self::template(),$headers);
}
你的问题的关键是面向对象编程。将包含文件结构化为 class,您可以在包含它后立即对其进行初始化。然后,您将能够直接访问您尝试获取的所有属性(如果它们是 public),或者通过一些获取方法。然后,您还可以通过将模板 html 放入另一个 get 方法来取消输出缓冲。像...
return $templateObj->getHTML();
使每个模板中的 class 相同,或者以类型命名它以简化您的 class 初始化。
$templateObj = new Template();
//or
$templateObj = new $YourTemplateName();
//where $YourTemplateName == "Account_Verify" or whichever template you're loading