S
S
Sergey Pugovkin2017-03-21 19:40:43
PHP
Sergey Pugovkin, 2017-03-21 19:40:43

What is the difference between self and "ClassName"?

class A {
    public static $var = 1;
    public function hello() {
        echo self::$var;
        echo A::$var;
    }
}

How will self::$var and A::$var differ here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Netesin, 2017-03-21
@Driver86

A::$var
call in the context of class A
call in the context of a class in which there is definitely (in this case class A)
a call in the context of the current class,
i.e. in the case of inheritance:
- if you have a class B that is inherited from class A and in which $ will be redefined var, it will be used,
- in the case of self, the variable will be used from where self is called
, i.e. nothing in your particular case, but it is better to use static

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question