Home PHP pass values from php file to other class
Reply: 4

PHP pass values from php file to other class

Liam Swipe
1#
Liam Swipe Published in 2018-01-12 10:34:32Z

I am using a config.php file for a simular something, im trying to put my mysql credentials in there to then use them in a different file, but it does not pass the values,

is there any1 that could help me find a solution.

code config.php:

/* Database credentials*/
$dbHost = 'localhost';
$dbName = 'xx';
$dbUsername = 'xx';
$dbWachtwoord = 'xx'; 

code dbconnect.php:

<?php include 'config.php';
class Database
{
    private $host;
    private $db_name;
    private $username;
    private $password;
    public $conn;

    public function dbConnection()
    {
        $this->host = $dbHost;
        $this->db_name = $dbName;
        $this->username = $dbUsername;
        $this->password = $dbWachtwoord;
        $this->conn = null;
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}

Class.user dbconnection:

<?php
require_once('config.php');
require_once('dbconnect.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

Thanks in advance =)

halojoy
2#
halojoy Reply to 2018-01-12 10:43:35Z

Include 'config.php inside your class

 public function dbConnection()
    {
        include 'config.php';
        $this->host = $dbHost;
        $this->db_name = $dbName;
        $this->username = $dbUsername;
        $this->password = $dbWachtwoord;
        $this->conn = null;
        try
        {
Mirsidikov Akbar
3#
Mirsidikov Akbar Reply to 2018-01-12 10:45:07Z

USE include_once() or require_once() instead to call config.php and in config.php file

class Config{
   $dbname='xx';
   $dbwatchwoord='xx';
   ...
}

and

then include config.php file

$config=new Config();
$config->dbname;
$config->dbpass;

try this...

Tomasz
4#
Tomasz Reply to 2018-01-12 10:41:14Z

Rather than thinking about it as passing variables, think about it as passing configuration. It is necessary for your Database class to be aware of those configuration options in order for it to be used. In other words: once you create an instance of class Database it should be configured and ready to use, just like any service would. I strongly suggest you follow the rule of injecting the configuration as a dependency.

ravisachaniya
5#
ravisachaniya Reply to 2018-01-12 10:52:56Z

You can try below code :

config.php

<?php 
return [
    'dbHost' => 'localhost',
    'dbName' => 'xx',
    'dbUsername' => 'xx',
    'dbWachtwoord' => 'xx',
];

user class

class USER
{   

    private $conn;
    private $config;

    public function __construct()
    {
        $this->config = include "config.php";
        $database = new Database(this->config['dbHost'], $this->config['dbUsername'], $this->config['dbWachtwoord'], $this->config['dbName']);
        //...
    }
    //...
}

dbconnect.php

public function dbConnection($dbHost, $dbName, $dbWachtwoord, $dbName)
{
    //....
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.255107 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO