PHP从静态函数更改变量

时间:2013-11-04 19:51:26

标签: php class oop static

我已经开始构建这个类,我想注册一个用户:

<?php
class User {
    protected $_id;
    protected $_name;
    protected $_email;
    protected $_password;
    public $isLogged = false;
    public $errors = array();

    public function __construct() {

    }
    public static function register($username,$email,$password,$captcha,$agree) {
        $user = new self;
        array_push($user->errors,'Error!');
    }
}

我称之为:

$user = User::register($_POST['username'],$_POST['email'],$_POST['password'],$captcha,$agree);
if(empty($user->errors)) {
    echo 'Success';
} else {
    echo 'Failed';
}

为什么会返回Success?我做了array_push

1 个答案:

答案 0 :(得分:3)

class User {

    // ...

    public static function register($username,$email,$password,$captcha,$agree) {
        $user = new self;
        array_push($user->errors,'Error!');
        return $user;
    }
}

您忘记从$user返回register()对象。

相关问题