简体   繁体   English

php方法在没有初始化对象的情况下访问意外错误

[英]php method access unexpected error without initialize object

I am trying to create a user class in php but I have a problem in accessing the method before create the object with and without static我正在尝试在php创建一个user类,但是在使用和不使用static创建对象之前访问该方法时遇到问题

<?php

class user{

    private $firstName;
    private $lastName;
    private $email;
    private $password;

    public function __construct($firstName,$lastName,$email,$password)
    {
        $this->$firstName=$firstName;
        $this->$lastName=$lastName;
        $this->$email=$email;
        $this->$password=$password;
        echo "create user";
    }

    public static function login($email,$password)
    {
        echo "login";
        if ( ! isset(self::$_instance)) {
            self::$_instance = new self();
        }

        $this->$email=$email;
        $this->$password=$password;

       /*
        login data
       */

        return self::$_instance;
    }
}

$mz= new user("mz","z","a@z.v","zxfef");
$mm= new user::login("mz","vc");
?>

for the last line I got an error:对于最后一行,我收到一个错误:

Parse error: syntax error, unexpected 'login' (T_STRING), expecting variable (T_VARIABLE) or '$'解析错误:语法错误,意外的“登录”(T_STRING),需要变量(T_VARIABLE)或“$”

login() is a static method, so you need to access it directly on the class using the Scope Resolution Operator( :: ). login()是一个静态方法,因此您需要使用范围解析运算符( :: ) 直接在类上访问它。 You can't use new in connection to static methods.您不能将new与静态方法结合使用。 new is only for instantiating objects of this class. new仅用于实例化此类的对象。

$mm = user::login("mz","vc"); // remove new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM