dongyisa6254 2016-04-14 18:14
浏览 36
已采纳

PHP中的类调用函数

I have a function with couple of variables in the external file

include 'external.php'; 

and it has:

function external ($username){
    $subjectreq = "Subject";
    $message = '
        <html>
            <head>
                <title>Hi '.$username.'</title>
            </head>
            <body>
                <p>Hi '.$username.'</p>
                <table style="background: #fff;marg in: 2px;padding: 5px 10px;border:1px solid #999;">
                    <tr>
                      <td>Somebody would like to add you among friends.</td>
                    </tr>                           
                    <tr>
                      <td>Please visit this site to accept the request.</td>
                    </tr>                               
                </table>
            </body>
        </html>';
    }

Then I created a class where I'm trying to call this function but it is not working:

include 'external.php'; 

    class FriendLoad {
          public function __construct(){
             add_friend();
          }

        private function add_friend(){

            // select user email and names
            $select = $this->db_connection->prepare("SELECT * FROM  users WHERE user_id= :friend_user_id");
            $select->bindParam(':friend_user_id', $friend_user_id, PDO::PARAM_INT);
            $select->execute();
            $to = "";
            $username = "";
            while($row = $select->fetch(PDO::FETCH_ASSOC)) { $to .= $row['user_email']; $username .= $row['user_name']; }

             external($username);

            $headers  = 'MIME-Version: 1.0' . "
";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
"; 
            $headers .= 'From: "Bla" <bla@bla.com>' . "
" ;
            mail($to, $subjectreq, $message, $headers); 
        }
    }
    $friendload = new FriendLoad();

I'm not sure why calling function external(); this way isn't working. I'm getting undifined variables $message and $subjectreq. Any idea?

  • 写回答

2条回答 默认 最新

  • dqqy64515 2016-04-14 18:23
    关注

    The code you have posted is working properly. What I think is missing is that you are not returning $message from the function. Try adding this to the bottom of the external() function:

    return $message;
    

    And in your class, add this before the call:

    $message = external($username);
    

    I should also point out Vincent G's answer as well. You are in the scope of your class when you call add friend, so you need to add $this->add_friend() so that PHP knows you want to use the current class's method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化