【问题标题】:Variable scope in PHP classPHP类中的变量范围
【发布时间】:2013-05-12 15:43:42
【问题描述】:

我的问题很简单,但我无法解决。

在我的 index.php 中,我包含了两个 PHP 文件。

    require_once("/lib/config.php");
    require_once("/lib/connect.php");

在配置文件中我声明变量#config

$config = array(  
    "db" => array(  
        "www_db" => array(  
            "username" => "user1",  
            "password" => "pass1",  
            "conn_string" => "blabla"
        )  
    ),  
    "paths" => array("images" => $_SERVER["DOCUMENT_ROOT"] . "/images")  
);  

在 connect.php 中我有一个单例类 Connection。

    class Connection
    {

private static $instance = NULL;

public static function getInstance()
{
    if (!self::$instance)
        self::$instance = new Connection();
    return self::$instance;
}


private $conn;

// Create connection to Oracle
public function getConnection() 
{
    //if (INCLUDE_CHECK == true)
//  {
        $conn = oci_connect($this -> $config["db"]["www_db"]["username"], 
                            $this -> $config["db"]["www_db"]["password"], 
                            $this -> $config["db"]["www_db"]["conn_string"]);

我的问题是我的 Connection 类没有看到在 config.php 中声明的 $config 变量。我也尝试将 $config 声明为全局。我收到错误“未定义的变量:配置...”“...在connect.php中”。 请帮忙。

【问题讨论】:

  • 如果你包含一个带有变量的文件,你不能使用$this,因为它引用了当前对象的属性。

标签: php


【解决方案1】:

你看不到类内部的变量,因为它是在外部声明的。传给班级, 使用 依赖注入 (either setter or constructor injection) 然后$config 将在您的连接类中可用。

【讨论】:

  • 嗨,肖恩,是的,除了需要数据库信息的方法都是静态的(基本上是备用构造函数)之外,这会起作用我可以将数据库信息作为 arg 传递给这些,但为了简单,我使用 global 关键字将它们从全局范围中拉入。 (见下面陈的回答)。不过谢谢!
【解决方案2】:

您必须使用global 关键字来指定何时要包含全局变量。 例如,它是这样工作的:

$config = array ('one' => 'two');

class Foo {
    public function bar() {
        print_r($config); // NULL
        global $config;
        print_r($config); // Array ( 'one' => 'ywo' )
    }
}

【讨论】:

  • 谢谢陈!这就是我解决它的方法,因为我在静态方法中使用这些,所以构造函数注入和 setter 方法对我没有用。
【解决方案3】:

我建议在 Connection.class 中添加一个新的 setter 方法,例如:

public function set_config($config = array()) {
    if (empty($config)) return false;

    $this->_username = $config["username"];
    $this->_password = $config["password"];
    // ... 
}

然后您可以在其他方法中使用这些数据,例如:

oci_connection($this->_username, $this->_password);

否则你设置 $config 全局:

global $config;

【讨论】:

  • @Havelock 你是对的,但我猜异常处理是另一章——我尽量让示例代码尽可能简单。 :-)
  • 就像我写的那样,我使用语句 global $config 但仍然看不到 $config 变量。
  • 你试过setter方法了吗?
  • @Igor 如果可能,请发布解决方案。它可以帮助任何有同样问题的人。谢谢!
【解决方案4】:

我是这样解决的:

private $username = "usr1";
private $password = "pass1";
private $conn_string = "connection_string";

private $conn;

// Create connection to Oracle
public function getConnection() 
{
    $conn = oci_connect($this -> username, 
                        $this -> password, 
                        $this -> conn_string);
    if (!$conn) {
       $m = oci_error();
       echo $m['message'], "\n";
       exit;
    }
    else
    {
        return $conn;
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2013-07-28
    • 1970-01-01
    • 2012-01-19
    • 2012-09-10
    • 2010-12-19
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多