0

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

这是我在 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();
    ?>

为什么这不起作用?

4

2 回答 2

2

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

于 2013-07-28T22:45:49.650 回答
1

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

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

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

正如 adpalumbo 所说,你打错了__construct

于 2013-07-28T22:44:49.150 回答