【问题标题】:PHP change variables from static functionPHP从静态函数更改变量
【发布时间】:2013-11-04 19:51:25
【问题描述】:

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

<?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

【问题讨论】:

  • 你没有在register()返回任何东西

标签: php class oop static


【解决方案1】:
class User {

    // ...

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

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

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 2012-04-05
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多