TheRebellion Posted February 25 Share Posted February 25 (edited) 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 February 25 by TheRebellion Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/ Share on other sites More sharing options...
requinix Posted February 25 Share Posted February 25 In variables.php, use constants instead of variables. Because constants can be used as default values while variables cannot. Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/#findComment-1616536 Share on other sites More sharing options...
TheRebellion Posted February 25 Author Share Posted February 25 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"; Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/#findComment-1616553 Share on other sites More sharing options...
TheRebellion Posted February 25 Author Share Posted February 25 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. Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/#findComment-1616554 Share on other sites More sharing options...
mac_gyver Posted February 25 Share Posted February 25 what is your overall goal here? OOP is not about wrapping your main code in class(es), adding $var-> in front of everything, and making a wall of code that takes 10x the number of lines of code to accomplish a task. Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/#findComment-1616565 Share on other sites More sharing options...
TheRebellion Posted February 25 Author Share Posted February 25 (edited) @mac_gyver - trying to create a class to use to connect to the database. I was able to get it to work. Edited February 25 by TheRebellion Quote Link to comment https://forums.phpfreaks.com/topic/318485-importing-values-into-class/#findComment-1616567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.