0

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' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
            $headers .= 'From: "Bla" <bla@bla.com>' . "\r\n" ;
            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?

Matthew R.
  • 4,332
  • 1
  • 24
  • 39
Katerpiler
  • 119
  • 8
  • You could try wrapping external in a class a making new External – Agu V Apr 14 '16 at 18:18
  • 1
    Is the line `include 'external.php';` also in the same file/included in the same file as the class FriendLoad? – Daniel Apr 14 '16 at 18:19
  • your question is not clear enough. where have you been added the include 'external.php'; Are these two files in a same directory path? – Dinidu Hewage Apr 14 '16 at 18:22
  • Your function does not `echo` or `return` anything, so it is probably executing, but it does not show. Try putting `return $message;` at the end of the function, and then save that to your `$message` in your class - `$message = external($username);`. This is a basic [variable scope (http://php.net/manual/en/language.variables.scope.php)](http://php.net/manual/en/language.variables.scope.php) issue – Sean Apr 14 '16 at 18:25
  • 1
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Sean Apr 14 '16 at 18:26
  • 1
    `$subjectreq` and `$message` are private to function `external` you cannot access it from anywhere outside that function; – bansi Apr 14 '16 at 18:28
  • @Sean, you are right. I didn't know you can have variable to equal to a function – Katerpiler Apr 14 '16 at 18:45

2 Answers2

4

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.

Community
  • 1
  • 1
Matthew R.
  • 4,332
  • 1
  • 24
  • 39
0

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;

Vincent G
  • 8,547
  • 1
  • 18
  • 36