Possible Duplicate:
New self vs. new static可能的重复:新的自我与新的静态
What is the difference between using self
and static
in the example below?
在下面的例子中,使用self和static有什么区别?
class Foo
{
protected static $bar = 1234;
public static function instance()
{
echo self::$bar;
echo "\n";
echo static::$bar;
}
}
Foo::instance();
produces
1234
1234
2 个解决方案
#1
133
When you use self
to refer to a class member, you're referring to the class within which you use the keyword. In this case, your Foo
class defines a protected static property called $bar
. When you use self
in the Foo
class to refer to the property, you're referencing the same class.
当您使用self引用一个类成员时,您是在引用使用关键字的类。在本例中,Foo类定义了一个受保护的静态属性$bar。当您在Foo类中使用self引用属性时,您引用的是同一个类。
Therefore if you tried to use self::$bar
elsewhere in your Foo
class but you had a Bar
class with a different value for the property, it would use Foo::$bar
instead of Bar::$bar
, which may not be what you intend:
因此,如果您尝试在Foo类的其他地方使用self: $bar,但是您有一个bar类,该属性的值不同,那么它将使用Foo::$bar而不是bar::$bar,这可能不是您想要的:
class Foo
{
protected static $bar = 1234;
}
class Bar extends Foo
{
protected static $bar = 4321;
}
When you use static
, you're invoking a feature called late static bindings (introduced in PHP 5.3).
当您使用静态时,您正在调用一个名为late static bindings的特性(PHP 5.3中引入)。
In the above scenario, using static
instead of self
will result in Bar::$bar
being used instead of Foo::$bar
, because the interpreter then takes into account the redeclaration within the Bar
class.
在上面的场景中,使用静态而不是self将导致Bar: $ Bar而不是Foo::$ Bar,因为解释器会考虑Bar类中的重新声明。
You typically use late static bindings for methods or even the class itself, rather than properties, as you don't often redeclare properties in subclasses; an example of using the static
keyword for invoking a late-bound constructor can be found in this related question: New self vs. new static
通常对方法甚至类本身使用后期静态绑定,而不是属性,因为您不经常在子类中重新声明属性;在这个相关的问题中可以找到使用静态关键字来调用后期绑定构造函数的示例:New self vs. New static
However, that doesn't preclude using static
with properties as well.
然而,这并不排除在属性中使用静态。
#2
33
self - refers to the same class whose method the new operation takes place in.
static - in PHP 5.3's late static binding refers to whatever class in the hierarchy which you call the method on.
In the following example (see get_called_class()), class B inherits both methods from class A. Self is bound to A because it's defined in A's implementation of the method, whereas static is bound to the called class despite the fact that it's also in A's implementation of the method.
在下面的例子中(参见get_called_class()), B类继承了来自类A的两种方法,因为它是在方法的实现中定义的,而静态则绑定到被调用的类,尽管它也在方法的实现中。
class A {
public static function get_A() {
return new self();
}
public static function get_me() {
return new static();
}
}
class B extends A {}
echo get_class(B::get_A()); // A
echo get_class(B::get_me()); // B
echo get_class(A::get_me()); // A
#1
133
When you use self
to refer to a class member, you're referring to the class within which you use the keyword. In this case, your Foo
class defines a protected static property called $bar
. When you use self
in the Foo
class to refer to the property, you're referencing the same class.
当您使用self引用一个类成员时,您是在引用使用关键字的类。在本例中,Foo类定义了一个受保护的静态属性$bar。当您在Foo类中使用self引用属性时,您引用的是同一个类。
Therefore if you tried to use self::$bar
elsewhere in your Foo
class but you had a Bar
class with a different value for the property, it would use Foo::$bar
instead of Bar::$bar
, which may not be what you intend:
因此,如果您尝试在Foo类的其他地方使用self: $bar,但是您有一个bar类,该属性的值不同,那么它将使用Foo::$bar而不是bar::$bar,这可能不是您想要的:
class Foo
{
protected static $bar = 1234;
}
class Bar extends Foo
{
protected static $bar = 4321;
}
When you use static
, you're invoking a feature called late static bindings (introduced in PHP 5.3).
当您使用静态时,您正在调用一个名为late static bindings的特性(PHP 5.3中引入)。
In the above scenario, using static
instead of self
will result in Bar::$bar
being used instead of Foo::$bar
, because the interpreter then takes into account the redeclaration within the Bar
class.
在上面的场景中,使用静态而不是self将导致Bar: $ Bar而不是Foo::$ Bar,因为解释器会考虑Bar类中的重新声明。
You typically use late static bindings for methods or even the class itself, rather than properties, as you don't often redeclare properties in subclasses; an example of using the static
keyword for invoking a late-bound constructor can be found in this related question: New self vs. new static
通常对方法甚至类本身使用后期静态绑定,而不是属性,因为您不经常在子类中重新声明属性;在这个相关的问题中可以找到使用静态关键字来调用后期绑定构造函数的示例:New self vs. New static
However, that doesn't preclude using static
with properties as well.
然而,这并不排除在属性中使用静态。
#2
33
self - refers to the same class whose method the new operation takes place in.
static - in PHP 5.3's late static binding refers to whatever class in the hierarchy which you call the method on.
In the following example (see get_called_class()), class B inherits both methods from class A. Self is bound to A because it's defined in A's implementation of the method, whereas static is bound to the called class despite the fact that it's also in A's implementation of the method.
在下面的例子中(参见get_called_class()), B类继承了来自类A的两种方法,因为它是在方法的实现中定义的,而静态则绑定到被调用的类,尽管它也在方法的实现中。
class A {
public static function get_A() {
return new self();
}
public static function get_me() {
return new static();
}
}
class B extends A {}
echo get_class(B::get_A()); // A
echo get_class(B::get_me()); // B
echo get_class(A::get_me()); // A