【问题标题】:how to use another file inside a php class如何在 php 类中使用另一个文件
【发布时间】:2017-08-24 21:54:04
【问题描述】:

我有两个文件 dbconnect.php 和 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',
  ];
?>

在我的 dbconnect.php 文件中;
如何将 config.php 中的变量包含到连接类中

如果我按照上面的方式进行操作;
它对我大喊大叫并给我致命错误:
“解析错误:语法错误,第 8 行 C:\xampp\htdocs\hngfun\profile\adeojoemmanuel\php-handler\dbconfig.php 中的意外 '$config' (T_VARIABLE)”

【问题讨论】:

  • 最重要的是dbconfig.php的代码没有显示
  • 不能为private $dbhost 等声明的属性分配依赖于运行时数据的值,例如$config['host'];
  • 文件dbconfig.php在哪里?

标签: php sql database


【解决方案1】:

我在这里猜测一下。但是我可以清楚地看到您将 $config 设置为构造函数中的局部变量。这意味着一旦您离开构造函数,它就无法使用。

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

【讨论】:

    【解决方案2】:

    不能为private $dbhost 等声明的属性分配依赖于运行时数据的值,例如$config['host'];

    引用PHP Docs:

    这个声明可以包含一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时被评估,并且不能依赖运行时信息才能被评估。

    解决方案,在构造函数中赋值:

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

    【讨论】:

      【解决方案3】:

      你需要在构造函数中设置:

      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'];
      
      }
      

      【讨论】:

        【解决方案4】:

        第一个问题是您不能只从 config.php 文件中返回任何内容。您只能在函数内返回结果。实现这一点的一种方法是将数组声明为全局变量,然后在需要该配置数组的所有其他 php 文件中使用它。

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

        【讨论】:

        • 我也是这么想的。事实证明,您可以从 require 中返回全局范围。不推荐这样做的技术,但你可以做到。
        • include/require 确实允许可以分配的返回,如php docs 中所述,并且不限于“全局”范围......It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function. ...事实上, Laravel 在其配置文件中使用这种方法
        • 对我来说这是一个多么美好的夜晚,我也学到了一些新东西。感谢分享这个:)
        猜你喜欢
        • 2012-12-30
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-19
        • 1970-01-01
        • 2014-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多