Undefined variable from required file

A

Anonymous

Guest
I'm using wamp. I have Data.php and Config.php in the same folder. I have required Config.php in Data.php
Config.php
Code:
    private $host;
    private $dbname;
    private $user;
    private $pass;

    public function Connect()
    {
        $this->host = "localhost";
        $this->dbname = "core";
        $this->user = "root";
        $this->pass = "";
        $this->charset = "utf8nb4";

        try 
        {
            $dsn = "mysql:host=".$this->host.";dbname=".$this->dbname.";charset=".$this->charset;
            $pdo = new PDO($dsn, $this->user, $this->pass);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, ERRMODE_EXCEPTION);
            return $pdo;
        } 
        catch (PDOException $e) 
        {
            echo "Connection failed: ". $e->getMessage();
        }
    }

Data.php
Code:
require_once('Config.php');
Code:
public function __construct()
    {
        try 
        {
            $pdo = new PDO($dsn,$user,$pass);
            $pdo->connect();
        }
        catch (PDOException $e) 
        {
            echo 'ERROR: ' . $e->getMessage();
        }
    }

I am getting Undefined variable for dsn, user and pass in Data.php. When I prepare a mysql statement in Data.php, I get call to member function prepare() on null. What am I doing wrong? Any help appreciated.
 
I seemed to have fixed the problem between Data.php and Config.php I don't have any errors or warnings in Data.php
Config.php
Code:
class Db
{
    public function connect()
    {

        $this->host = 'localhost';
        $this->user = 'root';
        $this->pass = '';
        $this->dbName = 'core';

        try {
            $dsn = 'mysql:host=' . $this->host .';dbName=' . $this->dbName;
            $pdo = new PDO($dsn, $this->user, $this->pass);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            return $pdo;
        } 
        catch (PDOException $e) 
        {
            echo 'Error' . $e->getMessage();
        }
    }
}

$db = new DB;

Data.php
Code:
require_once('Config.php');
$data = new Data;

class Data{

    public function Db()
    {
        try 
        {
            $pdo = new PDO($db->dsn, $db->user, $db->pass);
        } 
        catch (PDOException $e) 
        {
            echo 'Error' . $e->getMessage();
        }    
    }
    
    public Function Email()
    {
    //code
    }
}

In Config.php I have
Code:
include_once('Data.php');

class Logic
{
    
    public function EmailVerify()
    {
        try 
        {
            $registered = $data->Email();
            if($registered)
            {
                echo "Registered";
            }
        } 
        catch (PDOException $e) 
        {
            echo 'Error: ' . $e->getMessage();
        }
       
    }
I'm getting data as undefined variable in Logic.php
Any help appreciated.
 
This is my construct in Data.php which requires Config.php
Code:
require 'Config.php';
$data = new Data($db);

class Data
{
    public function __construct(PDO $db)
    {
        $this->db = $db;
    } 
    }

This is Config.php
Code:
class Db extends PDO 
{
    private $host = 'localhost';
    private $dbName = 'core';
    private $user = 'root';
    private $pass = 'pass';

    public function Connect()
    {
        $dsn = "mysql:host=".$this->host.";dbName=".$this->dbName;
        try {
            // now we can call the parent PDO constructor:
            parent::__construct($dsn, $this->user, $this->pass);
            $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        } 
        catch (PDOException $e) 
        {
            echo 'Error: '.$e->getMessage();
        }
    }
}

Notice: Undefined variable: db in Data.php
Fatal error: Uncaught TypeError: Argument 1 passed to Data::__construct() must be an instance of PDO, null given in Data.php

I have instantiated the Data class in Data.php but I'm getting Undefined variable for dsn, user and pass in Data.php
If Config is required, why is the Data class recognizing those variables?
 
Back
Top