Jump to content

Recommended Posts

I have a file that is called databaseimports.php this file includes a server, username and password. The file does not get checked into git due to the .gitignore.  I would like to directly import the variables in classes/variables.php to the class and assign them to $username and $token in the class.

Is this possible? Am I missing something very simple? 

 

<?php
include_once "classes/variables.php";

class Connection {
    private  string $server = "mysql:host=localhost;dbname=test";
    private array $options  = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
    protected $con;
    private string $username = $user;
    private string $token = $pass;

    public function openConnection($username, $token) {
        try {
            $this->con = new PDO($this->server, $this->username,$this->token,$this->options);
            return $this->con;
        }
        catch (PDOException $e){
            echo "There is some problem in connection: " . $e->getMessage();
        }
    }

    public function closeConnection(): void{
        $this->con = null;
    }
}

 

Edited by TheRebellion
Link to comment
https://forums.phpfreaks.com/topic/318485-importing-values-into-class/
Share on other sites

 

7 hours ago, requinix said:

In variables.php, use constants instead of variables. Because constants can be used as default values while variables cannot.

Thank you.

The error in PHP Storm that I am getting is:

  • Undefined variable '$db_user'
  • Undefined variable '$db_token'

The error is being produced in test.php When I attempt to run it both db_user and db_token are blank. They are not returning any value. 

I have tried it a different way here are my files:

classes/variables.php

<?php
    return array (
        'db_user' => 'test',
        'db_token' => 'test'
    );

classes/Config.php

<?php

class Config {
    private $_config;
    public function __construct() {
        $this->_config = include('variables.php');
    }
    public function __get($key)  {
        if (isset($this->config[$key])) {
            return $this->_config[$key];
        }
        return null;
    }
}

classes/connection.php

<?php
class Connection {
    private  string $server = "mysql:host=localhost;dbname=test";
    private array $options  = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);


    protected $con;

    public function openConnection($user,$token) {
        try {
            $this->con = new PDO($this->server, $user, $token ,$this->options);
            return $this->con;
        }
        catch (PDOException $e){
            echo "There is some problem in connection: " . $e->getMessage();
        }
    }

    public function closeConnection(): void{
        $this->con = null;
    }
}

test.php

<?php
require_once "classes/Config.php";
require_once "classes/connection.php";

try {

    $config = new Config();
    $user = $config->$db_user;
    $token = $config->$db_token;


    $database = new Connection();

    $db = $database->openConnection($user,$token);

    echo "Connection Made";

} catch (PDOException $e) {

    echo "There is some problem in connection: " . $e->getMessage();

}

echo "<br />Test";

 

I have modified classes.Config.php to be the following:

 

<?php

class Config {
    private $_config;
    public function __construct() {
        $this->_config = include('variables.php');
    }
    public function __get($key)  {
        if (isset($this->_config[$key])) {
            return $this->_config[$key];
        }
        return null;
    }
}

I am getting the same error but did see that user error in typing. 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.