【问题标题】:PHP variable scope within classes类中的 PHP 变量范围
【发布时间】:2013-07-28 15:11:59
【问题描述】:

我正在尝试创建一个简单地保存和检索简单字符串变量的类。

这是我在 User.php 文件(类)中的内容

class User {

//private variables
private $u_s;
private $p_w;

public function _construct($u_s, $p_w){
    $this->u_s = $u_s;
    $this->p_w = md5($p_w);
}

function getUsername(){
    return $this->u_s;
}

function getPassword(){
    return $this->p_w;
}

}

这里是 index.php 文件

    <?php
       include("User.php");

       $u = new User("Jake", "pass");
       echo $u->getUsername() + "\n" + $u->getPassword();
    ?>

为什么这不起作用?

【问题讨论】:

    标签: php class variables scope private


    【解决方案1】:

    你打错了__construct。应该有两个下划线,而不是一个。

    【讨论】:

      【解决方案2】:

      PHP 中的 Concat char 是 .(点),而不是 +(加号):

      <?php
      include("User.php");
      
      $u = new User("Jake", "pass");
      echo $u->getUsername() . "\n" . $u->getPassword();
      

      正如 adpalumbo 所说,你打错了 __construct

      【讨论】:

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