When I see a 'self' automatically to think of static methods. Lately I have been pointed out that self depends on the context. Just like 'parent', which can also call static methods. Consider this example:
error_reporting(-1);
class A
{
public $var = 1;
public function __construct($n)
{
开发者_开发技巧 $this->var = $n;
self::foo();
}
public function foo()
{
echo $this->var;
}
}
$obj = new A(5);
Operate without errors and within the method foo $ this is available. Someone can tell me some guide that explains in detail how the calls are resolved by self and parent?
$this
$is a reference to the current object, while self
is the reference to the class where it is used.
An example - the result of the code below is: (B::func)(A::func)
.
class A {
function call() {
$this->func();
self::func();
}
function func() {
echo '(A::func)';
}
}
class B extends A {
function func() {
echo '(B::func)';
}
}
$b = new B();
$b->call();
精彩评论