Php 为什么赢了';我的类变量不是由我的_构造函数初始化的吗?

Php 为什么赢了';我的类变量不是由我的_构造函数初始化的吗?,php,Php,我正在学习使用_construct函数初始化类。。。我似乎走在正确的道路上,但在调试时,类变量返回空 代码如下: 类Access.php class Access { var $username=''; var $password=''; var $result=false; var $error=''; /** OBJECT CONSTRUCTOR FUNCTION / CREATES ACCESS OBJECT**/ public func

我正在学习使用_construct函数初始化类。。。我似乎走在正确的道路上,但在调试时,类变量返回空

代码如下:

类Access.php

class Access {
    var $username='';
    var $password='';
    var $result=false;
    var $error='';


    /** OBJECT CONSTRUCTOR FUNCTION / CREATES ACCESS OBJECT**/
    public function _construct($username, $password) {

        $this->username = $username;
        $this->password = $password;

    }
}
和我的测试代码:

include "Access.php";

$un = 'FakeUN';
$pw = 'FakePW';

$a = new Access($un,$pw);

echo $a->username;
echo $a->password;
这不会在屏幕上打印任何内容;类变量未初始化

如果你能让我知道我做错了什么,我将不胜感激


谢谢你

你必须写两个
\u

你只是忘了写两个下划线

所以写下:

public function __construct($username, $password) {
    //do something

}

构造函数方法的名称是
\u construct
。所有魔术方法都以两个下划线开始