Undefined variable from required file

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
richarddunnebsc
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Aug 31, 2017 3:48 am

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: Select all

    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: Select all

require_once('Config.php');

Code: Select all

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.
richarddunnebsc
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Aug 31, 2017 3:48 am

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: Select all

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: Select all

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: Select all

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.
richarddunnebsc
New php-forum User
New php-forum User
Posts: 10
Joined: Thu Aug 31, 2017 3:48 am

This is my construct in Data.php which requires Config.php

Code: Select all

require 'Config.php';
$data = new Data($db);

class Data
{
    public function __construct(PDO $db)
    {
        $this->db = $db;
    } 
    }
This is Config.php

Code: Select all

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?
Post Reply