【问题标题】:How to pass variable values from an included file to class properties如何将变量值从包含的文件传递到类属性
【发布时间】:2016-05-31 16:58:54
【问题描述】:

当我使用模板函数中的值时,我遇到了发件人姓名、主题和发件人电子邮件地址为空的问题,但如果我直接在邮件函数中写入电子邮件和主题,它们就会出现。

编辑

我要做什么

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);

}

【问题讨论】:

  • 类方法不会采用这样的值。将这些值定义为常量并在类方法中使用。
  • @EdwinAlex,我将无法在运行时从 template() 更改常量;
  • 显然,您的班级缺少 class 关键字和名称...
  • 您为什么决定不使用 PHP templating classes 之一?我使用一种使用PHP 作为模板语言的语言。他们已经为您完成了所有艰苦的工作。例如,Plates is a native PHP template system that’s fast, easy to use and easy to extend.。还有Twig
  • @RyanVincent 这是我第一次尝试这样做。您能否给出一个答案,建议使用该模板引擎将如何简化此操作?

标签: php class methods properties html-email


【解决方案1】:

问题的关键是面向对象编程。将您的包含文件构造成一个类,您可以在包含它后立即对其进行初始化。然后,您将能够直接(如果它们是公共的)或通过某些 get 方法访问您尝试获取的所有属性。然后,您还可以通过将模板 html 放入另一个 get 方法来取消输出缓冲。比如……

 return $templateObj->getHTML();

使每个模板中的类都一样,或者以类型命名以简化类初始化。

$templateObj = new Template();
//or
$templateObj = new $YourTemplateName();
//where $YourTemplateName == "Account_Verify" or whichever template you're loading

【讨论】:

  • 如何使用这种方法将参数从模板传递到电子邮件类?
  • 在包含后立即在包含的文件中声明该类。然后,您将拥有课程内容的范围。然后,您可以像现在一样将它们分配给电子邮件类,但更像是 $this->from_email = $templateObj->from_email;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2015-06-15
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多