2012-03-19 112 views
3

我的config.php这个...如何在PHP类中使用外部变量?

$dbhost = "localhost"; 

我希望能够使用一个类里面的$ DBHOST变量。

User.class.php

include 'config.php';  
class User { 
    private $dbhost = ??? 
    } 

有这样一对夫妇的其他问题,但他们对一些其他具体用途,我想这只是我真的不能找到别的一个基本的东西关于它在网上。

更新:哇,谢谢大家的帮助。这个网站是新手,但我可能会坚持下去,尽可能放弃。

回答

1

几个方面,我认为。 首先,它传递给类的构造函数:

include 'config.php';  
class User {   
    private $dbhost; 

    function __construct($dbhost){ 
     $this->dbhost=$dbhost; 
    } 
} 
$user= new User($dbhost); 

或者使用一个setter:

include 'config.php';  
class User {   
    private $dbhost; 

    function setDbhost($dbhost){ 
     $this->dbhost=$dbhost; 
    } 
} 
$user= new User(); 
$user->setDbhost($dbhost); 

或者使用常量:

define('DBHOST', 'localhost'); 
class User {   
    private $dbhost; 

    function __construct(){ 
     $this->dbhost=DBHOST; 
    } 
} 

或使用全球:

include 'config.php';  
class User {   
    private $dbhost; 

    public function __construct() { 
    global $dbhost; 
    $this->dbhost=$dbhost; 
} 
} 
+0

感谢所有的方式和例子。我最终使用了常量,因为我不想为每个实例都传递变量,也不想为每个实例调用setter函数。 – 2012-03-19 23:50:34

+0

@sajanNOPPIX我欢迎和高兴,我的帮助:) – Songo 2012-03-20 07:24:48

2

对于一些如DB主机,使用常量:

// define it first 
define('DBHOST', 'localhost'); 

// then anywhere you can use DBHOST: 
class User { 
    function __construct() { 
     echo DBHOST; 
    } 
} 

http://php.net/manual/en/language.constants.php

+1

虽然正确的,我的建议是做的不这样做。将所需的值注入课程中。 – MitMaro 2012-03-19 23:07:02

+0

这很好,谢谢。没有意识到这可以在课堂上发挥作用。 – 2012-03-19 23:32:12

+0

你是什么意思MitMaro? – 2012-03-19 23:32:55

4

你可以使用一个global variable,定义constant会更好,但使用的setter/getter方法是最好的。当你使用一个类时,它使用的任何外部资源应该被传递给它。如果您想进一步研究,此设计模式称为dependency injection

在这个例子中,我结合setter和getter成一个单一的方法,并包括要设置的值,当你第一次创建实例,使用构造的能力:

class User 
{ 
    private $host = null; 

    public function host($newHost = null) 
    { 
     if($newHost === null) 
     { 
      return $this->host; 
     } 
     else 
     { 
      $this->host = $newHost; 
     } 
    } 

    function __construct($newHost = null) 
    { 
     if($newHost !== null) 
     { 
      $this->host($newHost); 
     } 
    } 
} 

//Create an instance of User with no default host. 
$user = new User(); 

//This will echo a blank line because the host was never set and is null. 
echo '$user->host: "'.$user->host()."\"<br>\n"; 

//Set the host to "localhost". 
$user->host('localhost'); 

//This will echo the current value of "localhost". 
echo '$user->host: "'.$user->host()."\"<br>\n"; 

//Create a second instance of User with a default host of "dbserver.com". 
$user2 = new User('dbserver.com'); 

//This will echo the current value of "dbserver.com". 
echo '$user2->host: "'.$user2->host()."\"<br>\n"; 
0

如果你是计划使用变量(而不是常量),然后使用下面的代码。

在config.php

$dbhost = "localhost"; 

在User.class.php

include 'config.php';  
class User { 
    global $dbhost; 
} 
+0

我不会推荐使用全局变量,他们可以创建难以/不可能追踪的错误。 – vascowhite 2012-03-19 23:30:37

+0

即使我不喜欢使用全局变量,也很难使用它们。但我试图回答用户特别要求的内容。 – vivek 2012-03-19 23:43:13

+0

OP没有提到全局变量。 – vascowhite 2012-03-20 10:02:47