在使用自我,父母,静态时如何使用? [英] when using self, parent, static and how?
问题描述
如果现在我对ststtic有所了解,现在我意识到我什么都不懂.我很困惑,我很难理解,但我做不到.有人可以在使用self,parent,static以及如何使用时解释此程序 我所做的所有最小更改都会改变结果,否则我将无法理解发生了什么. 非常感谢..
If by now I understood a little in ststic Now I realize I do not understand anything. I'm so confused and I struggle to understand and I can not. Someone can explain this program when using self, parent, static and how All the smallest change I do changes the result without that I can not understand what's going on. thanks a lot ..
来自 http://docs.php.net/language.oop5.late-static-的代码绑定
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
?>
输出是:
A
C
C
推荐答案
You'll need to understand the concept of Late Static Binding, which determines when an identifier is bound to code/data. You can tell PHP to bind it early (self::
) or later (static::
).
将示例简化为两个类:
class A {
public static function foo() {
self::who(); // PHP binds this to A::who() right away
static::who(); // PHP waits to resolve this (hence, late)!
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
B::test();
这篇关于在使用自我,父母,静态时如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!