多个require_once文件

时间:2013-02-05 13:17:36

标签: php

我正在开发由其他开发人员开发的脚本。有一个警报系统发送电子邮件,每个模块都有自己的警报文件和警报文件require_once PHPMailer

<?php
    // assume this is 1.php
    //.......... some code above //
    $mail_title = 'Message Title';
    $mail_body = 'Message body with complete details what has been done here';    
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file
    //........... ajax and some response //
?>

一旦我require_once,它就会生成一封电子邮件并发送给你。

现在我必须更新模块中的PHP页面。该页面必须将数据发布到模块的3个不同的php页面。在该页面将数据发送到仅一个php页面VIA ajax之前,其他两个页面使用First one的自动递增ID。

由于复杂性,我想在文件1中包含其他两个文件

<?php
    // assume this is 1.php
    //.......... some code above //
    $mail_title = 'Message Title';
    $mail_body = 'Message body with complete details what has been done here';    
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file
    //........... ajax and some response //
    if($condition){
        include '2.php'; // <- this file again require_once 'module_alert.php'
        include '3.php'; // <- this file again require_once 'module_alert.php'
    }
?>

问题是我想发送所有3个步骤的警报,而且我也不想更改其他文件中的大量代码,因为我可能会破坏脚本。

在这种情况下,我有什么选择?

修改

module_alerts.php中有很多事情。邮件布局,表格图像,条件收件人,谁应该获得此模块的警报等。$ mail-&gt; send()函数,以便发送电子邮件给特定的接收者。

1 个答案:

答案 0 :(得分:2)

发送电子邮件的逻辑应该包含在一个函数中,然后您可以使用适当的参数在程序中的任何位置调用该函数。例如:

function sendMyEmail($recipient, $subject, $message)
{ ... }

您当然可以构建一个类,一系列函数,实际上是任何合法的可重用设施,这样可以减轻现在和将来的问题。

HTH