PHP 5:const 与 static
在 PHP 5 中,使用 const
和 static
有什么区别?
分别什么时候合适? public
、protected
和 private
扮演什么角色 - 如果有的话?
In PHP 5, what is the difference between using const
and static
?
When is each appropriate? And what role does public
, protected
and private
play - if any?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在类的上下文中,静态变量位于类范围(而不是对象)范围内,但与 const 不同,它们的值可以更改。
public、protected 和 private 在常量方面是不相关的(它们总是公共的);它们仅对类变量有用,包括静态变量。
编辑:重要的是要注意 PHP 7.1.0 引入了对指定类常量可见性的支持。
In the context of a class, static variables are on the class scope (not the object) scope, but unlike a const, their values can be changed.
Public, protected, and private are irrelevant in terms of consts (which are always public); they are only useful for class variables, including static variable.
Edit: It is important to note that PHP 7.1.0 introduced support for specifying the visibility of class constants.
应该指出的最后一点是 const 始终是静态的和公共的。这意味着您可以从类内部访问 const,如下所示:
从类外部您可以像这样访问它:
One last point that should be made is that a const is always static and public. This means that you can access the const from within the class like so:
From outside the class you would access it like this:
Constant 只是一个常量,即声明后不能更改其值。
静态变量无需创建类实例即可访问,因此在类的所有实例之间共享。
此外,函数中可以有一个仅声明一次(在函数第一次执行时)的静态局部变量,并且可以在函数调用之间存储其值,例如:
Constant is just a constant, i.e. you can't change its value after declaring.
Static variable is accessible without making an instance of a class and therefore shared between all the instances of a class.
Also, there can be a static local variable in a function that is declared only once (on the first execution of a function) and can store its value between function calls, example:
在谈论类继承时,您可以使用
self
和static
关键字来区分不同作用域中的常量或变量。检查这个示例,它说明了如何访问什么:然后执行:
或:
输出:
换句话说,
self::
引用来自调用它的同一范围的静态属性和常量(在此如果是Person
超类),而static::
将在运行时从作用域访问属性和常量(因此在本例中,在Pirate
子类)。阅读有关 php.net 上的后期静态绑定的更多信息.
另请检查此处和这里。
When talking about class inheritance you can differentiate between the constant or variable in different scopes by using
self
andstatic
key words. Check this example which illustrates how to access what:And then do:
or:
Output:
In other words
self::
refers to the static property and constant from the same scope where it is being called (in this case thePerson
superclass), whilestatic::
will access the property and constant from the scope in run time (so in this case in thePirate
subclass).Read more on late static binding here on php.net.
Also check the answer on another question here and here.
将类方法或属性声明为静态可以使它们无需实例化类即可访问。
类常量就像普通常量一样,它不能在运行时更改。这也是您使用 const 的唯一原因。
Private、public 和 protected 是访问修饰符,描述谁可以访问哪个参数/方法。
公共意味着所有其他对象都可以访问。
私有意味着只有实例化的类才能访问。
受保护意味着实例化的类和派生类可以访问。
Declaring a class method or property as static makes them accessible without needing an instantiation of the class.
A class constant is just like a normal constant, it cannot be changed at runtime. This is also the only reason you will ever use const for.
Private, public and protected are access modifiers that describes who can access which parameter/method.
Public means that all other objects gets access.
Private means that only the instantiated class gets access.
Protected means that the instantiated class and derived classes gets access.
这是我到目前为止学到的关于静态成员、常量变量和访问修饰符(私有、公共和受保护)的知识。
常量
定义
就像名字一样,常量变量的值不能改变。常量与普通变量的不同之处在于,你不使用 $ 符号来声明或使用他们。
该值必须是常量表达式,而不是(例如)变量、属性、数学运算的结果或函数调用。
在php中声明常量 常量
的作用域是全局的,可以使用self关键字访问
Static
定义
Static关键字可用于声明类、成员函数或变量。Static类中的成员是全局的,也可以使用 self 关键字进行访问。将类属性或方法声明为静态可以使它们无需实例化类即可访问。声明为静态的属性无法使用实例化的类对象进行访问(尽管静态方法可以)。如果未使用可见性声明( public、private、protected ),则该属性或方法将被视为已声明为 public。因为静态方法无需创建对象实例即可调用。
静态属性示例
访问静态属性和函数示例
公共、私有、受保护(AKA 访问修饰符)
在阅读下面的定义之前,请阅读这篇文章封装。它将帮助您更深入地理解概念
链接 1 维基百科
关于封装的教程点链接
定义
使用 private 、 public 、protected 关键字可以控制对类中成员的访问。声明为 public 的类成员可以在任何地方访问。声明为 protected 的成员只能在类本身内部以及继承类和父类中访问。声明为私有的成员只能由定义该成员的类访问。
示例
访问公共、私有和受保护成员示例
有关更多信息,请阅读有关可见性的 php 文档 Visibility Php Doc
参考资料:< a href="http://php.net" rel="nofollow">php.net
我希望你理解这个概念。感谢您的阅读 :) :) 祝您玩得愉快
Here's the things i learned so far about static members , constant variables and access modifiers(private,public and protected).
Constant
Definition
Like the name says values of a constant variable can't be changed.Constants differ from normal variables in that you don't use the $ symbol to declare or use them.
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
Declaring a constant in php
Constant's scope is global and can be accessed using a self keyword
Static
Definition
Static keyword can be used for declaring a class, member function or a variable.Static members in a class is global can be accessed using a self keyword as well.Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can). If no visibility declaration ( public, private, protected ) is used, then the property or method will be treated as if it was declared as public.Because static methods are callable without an instance of the object created.
Static property example
Accessing static properties and functions example
Public, private , protected (A.K.A access modifiers)
Before reading the definition below , read this Article about Encapsulation .It will help you to understand the concept more deeply
Link 1 wikipedia
Tutorials point link about encapsulation
Definition
Using private , public , protected keywords you can control access to the members in a class. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.
Example
Accessing the public, private and protected members example
For more info read this php documentation about visibility Visibility Php Doc
References : php.net
I hope you understood the concept. Thanks for reading :) :) Have a good one
因此,回顾一下@Matt很好的答案:
如果您需要的属性不应更改,那么常量是正确的选择
如果您需要的属性允许更改,请使用 static 代替
示例:
编辑: 需要注意的是 PHP 7.1.0 引入了对指定类常量的可见性。
So to recap on @Matt great answer:
if the property you need should not be changed, then a constant is the the proper choice
if the property you need is allowed to be changed, use static instead
Example:
Edit: It is important to note that PHP 7.1.0 introduced support for specifying the visibility of class constants.
我希望您重点关注使用 self:: 和 static:: 的一种不同情况,了解它也有助于调试:
i would like you focus on one case different in using self:: and static:: it is also help for debuging to be aware: