what does static mean?
静态是什么意思?
I know public means that it can be accessed from outside the class, and private only from inside the class
我知道public意味着它可以从类之外访问,而private仅从类内部访问
4 个解决方案
#1
34
Static means that it can be accessed without instantiating a class. This is good for constants.
静态意味着可以在不实例化类的情况下访问它。这对常数很好。
Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.
静态方法需要不影响对象的状态。除了参数之外,它们还可以有局部变量。
#2
21
public: Public declared items can be accessed everywhere.
公共:公共申报项目可在任何地方访问。
protected: Protected limits access to inherited and parent classes (and to the class that defines the item).
protected: protected限制访问继承和父类(以及定义该项目的类)。
private: Private limits visibility only to the class that defines the item.
private: private仅将可见性限制为定义项的类。
static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
静态:静态变量仅存在于本地函数范围内,但当程序执行离开该范围时,它不会失去其值。
final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
final: final关键字通过在定义前面加上final来防止子类重写方法。如果类本身被定义为final,那么它就不能被扩展。
transient: A transient variable is a variable that may not be serialized.
暂态变量:暂态变量是不能序列化的变量。
volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.
volatile:一个可以被多个线程同时修改的变量应该被声明为volatile。被声明为易失性的变量不会被编译器优化,因为它们的值可以随时改变。
#3
13
from http://php.net/manual/en/language.oop5.static.php
从http://php.net/manual/en/language.oop5.static.php
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
将类属性或方法声明为静态可以访问它们,而不需要实例化类。声明为静态的属性不能被实例化的类对象访问(尽管静态方法可以)。
#4
1
Some example ... When use static keyword then we cannot use $this..
一些例子…当使用静态关键字时,我们不能使用$this..
class Foo{
private $foo='private';
private function priv_func(){
echo 'priv_method';
}
public static function ger(){
echo $this->foo;
$this->priv_func();
}
}
//class Zero extends Foo{};
$obj=new Foo;
$obj->ger();
Fatal error: Using $this when not in object context in
致命错误:在对象上下文中不使用$this
#1
34
Static means that it can be accessed without instantiating a class. This is good for constants.
静态意味着可以在不实例化类的情况下访问它。这对常数很好。
Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.
静态方法需要不影响对象的状态。除了参数之外,它们还可以有局部变量。
#2
21
public: Public declared items can be accessed everywhere.
公共:公共申报项目可在任何地方访问。
protected: Protected limits access to inherited and parent classes (and to the class that defines the item).
protected: protected限制访问继承和父类(以及定义该项目的类)。
private: Private limits visibility only to the class that defines the item.
private: private仅将可见性限制为定义项的类。
static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
静态:静态变量仅存在于本地函数范围内,但当程序执行离开该范围时,它不会失去其值。
final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
final: final关键字通过在定义前面加上final来防止子类重写方法。如果类本身被定义为final,那么它就不能被扩展。
transient: A transient variable is a variable that may not be serialized.
暂态变量:暂态变量是不能序列化的变量。
volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.
volatile:一个可以被多个线程同时修改的变量应该被声明为volatile。被声明为易失性的变量不会被编译器优化,因为它们的值可以随时改变。
#3
13
from http://php.net/manual/en/language.oop5.static.php
从http://php.net/manual/en/language.oop5.static.php
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
将类属性或方法声明为静态可以访问它们,而不需要实例化类。声明为静态的属性不能被实例化的类对象访问(尽管静态方法可以)。
#4
1
Some example ... When use static keyword then we cannot use $this..
一些例子…当使用静态关键字时,我们不能使用$this..
class Foo{
private $foo='private';
private function priv_func(){
echo 'priv_method';
}
public static function ger(){
echo $this->foo;
$this->priv_func();
}
}
//class Zero extends Foo{};
$obj=new Foo;
$obj->ger();
Fatal error: Using $this when not in object context in
致命错误:在对象上下文中不使用$this