在yii2-basic模板中,对文件 models/User.php的理解有点困惑:
<span style="font-size:18px;"> public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new <span style="background-color: rgb(255, 255, 102);">static</span>(self::$users[$id]) : null;
}</span>
static是什么意思呢?与self又有什么不同呢?整理如下:
self refers to the same class whose method the new operation takes place in.
static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.
举个例子:
<span style="font-size:18px;">class A {
public static function get_self() {
return new self();
}
public static function get_static() {
return new static();
}
}
class B extends A {}
echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A</span>