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" <[email protected]>' . "\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?

7
  • You could try wrapping external in a class a making new External Commented Apr 14, 2016 at 18:18
  • 1
    Is the line include 'external.php'; also in the same file/included in the same file as the class FriendLoad? Commented Apr 14, 2016 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? Commented Apr 14, 2016 at 18:22
  • 1
    Possible duplicate of Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors? Commented Apr 14, 2016 at 18:26
  • 1
    $subjectreq and $message are private to function external you cannot access it from anywhere outside that function; Commented Apr 14, 2016 at 18:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Glad to help!
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;

1 Comment

well, you are right but I have it correctly like that, I just forgot to post it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.