class A {
const STR = "A";
public function x() {
echo self::STR;
}
}
class B extends A{
const STR = "B";
}
(new B)->x();
the final output is A. how do you understand this?
guess 1:
self will be bound to the current class at compile time, which can be understood as replacing all self with the class name at compile time.
so that after other classes inherit the parent class method, the method code is not self::xxx, but the parent class name:: xxx
, so the str in the example is the str in the parent class
conjecture 2:
the subclass inherits the parent method, in fact, it does not take the parent method, but has access to the parent method.
when calling a method that is not in the subclass, it will look for it in the parent class, and execute it in the parent class. Naturally, self points to the parent class
which of these two is right?
how do you understand self if it"s all wrong? First of all, thank you for your advice