【问题标题】:PHP pass values from php file to other classPHP将值从php文件传递给其他类
【发布时间】:2018-01-12 10:34:32
【问题描述】:

我将 config.php 文件用于类似的东西,我试图将我的 mysql 凭据放在那里,然后在不同的文件中使用它们,但它没有传递值,

有没有什么1可以帮助我找到解决方案。

代码配置.php:

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

代码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 数据库连接:

<?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;
    }

提前致谢 =)

【问题讨论】:

标签: php mysql class private


【解决方案1】:

与其将其视为传递变量,不如将其视为传递配置。您的数据库类必须了解这些配置选项才能使用它。换句话说:一旦您创建了类 Database 的实例,就应该对其进行配置并准备好使用,就像任何服务一样。 我强烈建议你遵守injecting the configuration as a dependency的规则。

【讨论】:

    【解决方案2】:

    在你的类中包含'config.php

     public function dbConnection()
        {
            include 'config.php';
            $this->host = $dbHost;
            $this->db_name = $dbName;
            $this->username = $dbUsername;
            $this->password = $dbWachtwoord;
            $this->conn = null;
            try
            {
    

    【讨论】:

      【解决方案3】:

      你可以试试下面的代码:

      config.php

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

      用户类

      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)
      {
          //....
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-11
        • 2020-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多