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?

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.

The error is here (I've tested it). You need to use $this to call the private function add_friend()

  public function __construct(){
     $this -> add_friend();
  }

Otherwise, your external file is good loaded.

And you also need a return of your var in external() function: return $message;