I'am trying to call the variable $conn in a method in my function. The variable contains PDO, but it's outside the scope. How would I be able to use it inside the function. Ive tried using GLOBALS but that doesn't seem to work like it does with normal variables. Also I've tried passing it with the function but that doesn't work either.
The SQL works and is tested and if I call $conn->exec() outside of the function it works as well.
class Friend{
public $firstName;
public $lastName;
public $sql;
public $conn;
function __construct($firstName, $lastName, $conn){
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->conn = $conn;
}
public function create(){
$this->sql = "INSERT INTO vrienden (Voornaam, Achternaam) VALUES ('$this->firstName', '$this->lastName')";
// echo $this->sql;
$stmt = $this->sql;
// Hieronder het daadwerkelijke uploaden van de vriend.
echo $stmt;
$conn->exec($this->stmt);
}
}