PHP OOP - 常量与静态变量? [英] PHP OOP - constant vs static variables?
问题描述
在PHP中,有什么区别:
In PHP, What is is the difference between:
- 常量和静态变量?
- 扩展一个类并创建它的对象?
我知道如何使用它们,但我无法清楚地区分它们。
I know how they can be used, but I can't clearly distinguish between them.
推荐答案
静态用于:
类属性或方法as static使它们无需访问类的实例化
class properties or methods as static makes them accessible without needing an instantiation of the class
因此,静态成员返回的值可能不同。例如,您可以根据传递给它的参数调用具有不同结果的静态方法。
So, the value returned by a static member may differ. For example, you can call a static method with different result depending of what parameters you pass to it.
常量值:
必须是常量表达式,而不是(例如)变量,属性,数学运算的结果或函数调用。
must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
所以,当你调用它时它总是返回相同的结果
So, it always return the same result when you call it
关于创建一个对象并扩展一个类,当你创建一个对象时,你创建一个一个班级。当您扩展课程时,您将创建另一个课程:
About create an object and extending a class, when you "create an object" you make an instance of a class. When you extend a class, you create an other class who:
从父类继承所有公共和受保护的方法。除非某个类重写这些方法,否则它们将保留其原始功能。
inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.
我希望它可以帮助你。
这篇关于PHP OOP - 常量与静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!