【问题标题】:How can i get a variable that was definied in class and use the it somewhere else如何获取在类中定义的变量并在其他地方使用它
【发布时间】:2012-10-24 19:19:31
【问题描述】:

我正在使用 codeignighter。

我有一个类有一些我需要在其他地方使用的变量。

例如:

 class details{

public $username;
public $current_uID;
public $member_status;

    public function __construct(){
         $this->username = 'username';
         $this->current_uID = 21;
         $this->member_status = 1;
    }
}

现在我的问题是我如何访问这些变量 - 如果我这样做,它们是否具有构造中定义的值?

【问题讨论】:

  • 这与 php 类的关系比与 codeigniter 的关系更大,对吧?只需创建该类的一个实例。

标签: php class codeigniter


【解决方案1】:

只需创建类并使用创建的实例的属性:

$details = new details();
echo $details->username;  // will put 'username' on the screen

【讨论】:

    【解决方案2】:
    $details = new details();
    
    $username = $details->username;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2019-09-12
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      相关资源
      最近更新 更多