简体   繁体   中英

how to use another file inside a php class

I've got two files dbconnect.php and config.php

dbconnect.php

 <?php 
class connect{
    public function __construct(){
        $config = require_once __DIR__ . 'config.php';
    }

    private $dbhost = $config['host'];
    private $dbuser = $config['username'];
    private $dbpass = $config['pass'];
    private $dbname = $config['dbname'];

    public function startConn(){
        $this->DBcon = null;
        try{
            $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass);
        }catch(Exception $e){
            echo "error connecting:";
        }
        return $this->DBcon;
    }
} 

?>

config.php

<?php

  /**
   * Contains all configurations
   *
  */
  return [
    'dbname' => 'user',
    'pass' => '@user.intern1',
    'username' => 'user1',
    'host' => 'localhost',
  ];
?>

in my dbconnect.php file;
how do I include variables from my config.php into the class connect

If I do it the following way above;
it yells at me and gives me Fatal error:
"Parse error: syntax error, unexpected '$config' (T_VARIABLE) in C:\\xampp\\htdocs\\hngfun\\profile\\adeojoemmanuel\\php-handler\\dbconfig.php on line 8"

I'm taking a guess here. But I can clearly see that you are setting $config as a local variable in the constructor. That means it is not available once you leave the constructor.

<?php 
class connect{
    public function __construct(){
        $config = require_once __DIR__ . 'config.php';
        $this->dbhost = $config['host'];
        $this->dbuser = $config['username'];
        $this->dbpass = $config['pass'];
        $this->dbname = $config['dbname'];
    }

    private $dbhost ;
    private $dbuser ;
    private $dbpass ;
    private $dbname ;

    public function startConn(){
        $this->DBcon = null;
        try{
            $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass);
        }catch(Exception $e){
            echo "error connecting:";
        }
        return $this->DBcon;
    }
} 

Declared properties like private $dbhost cannot be assigned values that are dependent on runtime data, such as $config['host'];

Quoting from the PHP Docs :

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

The solution, assign the values in your constructor:

class connect{
    public function __construct(){
        $config = require_once __DIR__ . 'config.php';

        private $this->dbhost = $config['host'];
        private $this->dbuser = $config['username'];
        private $this->dbpass = $config['pass'];
        private $this->dbname = $config['dbname'];
    }

    private $dbhost;
    private $dbuser;
    private $dbpass;
    private $dbname;

    public function startConn(){
        $this->DBcon = null;
        try{
            $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass);
        }catch(Exception $e){
            echo "error connecting:";
        }
        return $this->DBcon;
    }
} 

You need to set inside constructor:

private $dbhost;
private $dbuser;
private $dbpass;
private $dbname;

public function __construct(){
    $config = require_once __DIR__ . 'config.php';
    $this->dbhost = $config['host'];
    $this->dbuser = $config['username'];
    $this->dbpass = $config['pass'];
    $this->dbname = $config['dbname'];

}

The first problem is that you can't just return anything from your config.php file. You can only return a result inside a function. One way to implement this is to declare the array as a global variable and then use it inside all other php files that would need that configuration array.

<?php
/**
* Contains all configurations
*
*/
$dbconfig = array(
  'dbname' => 'user',
  'pass' => '@user.intern1',
  'username' => 'user1',
  'host' => 'localhost',
);
?>

<?php 
require_once __DIR__ . 'config.php';

class connect{
    public function __construct(){

    }

    private $dbhost = $dbconfig['host'];
    private $dbuser = $dbconfig['username'];
    private $dbpass = $dbconfig['pass'];
    private $dbname = $dbconfig['dbname'];

    public function startConn(){
        $this->DBcon = null;
        try{
            $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass);
        }catch(Exception $e){
            echo "error connecting:";
        }
        return $this->DBcon;
    }
} 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM