【问题标题】:How to use an outside variable in a PHP class?如何在 PHP 类中使用外部变量?
【发布时间】:2012-03-19 23:01:00
【问题描述】:

我有 config.php 这个...

$dbhost = "localhost";

我希望能够在类中使用 $dbhost 变量。

用户.class.php

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

还有其他几个类似的问题,但它们是针对其他特定用途的,我想这只是一个基本的东西,我在网上找不到任何关于它的其他信息。

更新:哇,感谢大家的帮助。刚接触这个网站,但我可能会留下来并尽我所能回馈。

【问题讨论】:

    标签: php class variables


    【解决方案1】:

    您可以使用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";
    

    【讨论】:

      【解决方案2】:

      对于数据库主机之类的东西,请使用常量:

      // 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

      【讨论】:

      • 虽然正确,但我的建议是不要这样做。将您需要的值注入到类中。
      • 效果很好,谢谢。没有意识到这会在课堂上起作用。
      【解决方案3】:

      我认为的几种方式。 首先将其传递给类构造函数:

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

      【讨论】:

      • 感谢您提供的所有方法和示例。我最终使用了常量,因为我不想为每个实例传递变量,也不想为每个实例调用一个 setter 函数。
      【解决方案4】:

      如果您打算使用变量(而不是常量),请使用以下代码。

      在 config.php 中

      $dbhost = "localhost";
      

      在 User.class.php 中

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

      【讨论】:

      • 我不建议使用全局变量,它们会产生难以/不可能追踪的错误。
      • 连我都不赞成使用全局变量,也很少使用。但我试图回答用户具体提出的问题。
      • OP没有提到全局变量。
      猜你喜欢
      • 2017-05-26
      • 2014-12-25
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 2010-11-05
      相关资源
      最近更新 更多