I am using wamp and have the following files:
Logic.php containing a Logic class which includes Data.php and creates new Data class object
`$data = new Data;
Data.php containing Data class which extends Db class and requires Config.php
Config.php contains Db class that connects to database
In Db class I have
[code]
class Db extends PDO
{
private $host;
private $dbName;
private $user;
private $pass;
private $charset;
public function Connect()
{
$this->host = “localhost”;
$this->user = “root”;
$this->pass = “”;
$this->dbName = “dbName”;
$this->charset = “utf8mb4”;
try
{
$dsn = “mysql:host=”.$this->host.”;dbName=”.$this->dbName.”;charset=”.$this->charset;
$pdo = new PDO($dsn, $user, $pwd, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
return $pdo;
}
catch (PDOException $e)
{
echo ‘Error: ‘.$e->getMessage();
}
}
}
In the Data class I have
[code]
require ‘Config.php’;
class Data extends Db
{
public function FunctionName()
{
try
{
if ($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$email = test_input($_POST[“email”]);
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = “Invalid email format”;
}
$sql = “Insert statement…..”;
$stmt= $pdo->prepare($sql);
$stmt->execute(values…..);
if($stmt)
{
return true;
}
}
catch (PDOException $e)
{
echo ‘Error: ‘ . $e->getMessage();
}
}
}
If I put this PDO construct into the Data class
[code]
public function __construct()
{
$pdo = new PDO($dsn,$user,$pass,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
I get Notice: Undefined variable: for dsn, user and pass.
Any help appreciated.