在 PHP 中声明静态函数有什么意义?
因此,在 PHP 中,您可以拥有
Class A{
function B(){}
}
并且可以像静态函数一样调用它:
A::B();
我的问题是...如果我可以做到这一点,那么为什么我应该将函数 B() 声明为静态函数,因为这样做会使 $this不可用,所以灵活性较差,所以你会失去一切,却一无所获……
或者将函数声明为静态是否有我不知道的优势?
我还听说“非静态方法的静态调用”已被“弃用”......这到底意味着什么,特别是在这种情况下?当 B() 未声明为静态时,正在调用 A::B()
,这是我不应该做的事情?如果是这样,为什么会这样?
So in PHP you can have
Class A{
function B(){}
}
and you can call this as if it were a static function:
A::B();
My question is...if I can do this, then why should I ever declare the function B() as static since doing so makes $this unavailable, so there's less flexibility, so you have everything to lose but nothing to gain...
or is there an advantage of declaring the function as static that I'm not aware of?
also I heard that "static calling of non static methods" are "deprecated"....what does that exactly mean especially in relation to this scenario? is calling A::B()
when B() is not declared static something that I shouldn't be doing? if so, why is that the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为 PHP 在严格性方面往往有点松散(?),所以这些事情是有效的。它们已被弃用的事实意味着在未来版本中的某个时候,它们可能不再不再工作。因此,如果您在静态上下文中调用非静态函数,您的程序可能会在未来的 PHP 升级中中断。
至于现在使用它 - 将函数声明为静态的优点是您可以立即决定如何使用该函数。如果您打算在静态上下文中使用函数,则无论如何都不能使用 $this,因此您最好清楚自己打算做什么。如果要静态使用该函数,请将其设置为静态。如果你不这样做,那就不要。如果您想静态和非静态地使用某个函数,请重新检查您的要求:P
Because PHP tends to be a bit loosy-goosy around strictness (?) these sort of things work. The fact that they are deprecated means that sometime in a future release, it is likely not to work anymore. So if you are calling a non-static function in a static context, your program may break in a future PHP upgrade.
As for using it right now - the advantage to declaring a function as static is that you are deciding right there how that function should be used. If you intend to use a function in a static context, you can't use $this anyway, so you are better of just being clear on what you plan to do. If you want to use the function statically, make it static. If you don't, then don't. If you want to use a function both statically and non-statically, then please recheck your requirements :P
对于兼容模式。现在,静态调用非静态方法会生成 E_STRICT 级别警告。
为什么静态而不实例化对象?每个程序员都会告诉你一个原因。我特别喜欢实例化对象而不是使用静态方法。它清晰、可追溯且更可重复使用。
我做了一个测试台,实例化和调用方法与静态调用方法之间的差异很小。
提示:如果您预见调用方法也会静态定义它们;-)
For compatibility mode. Now calling non-static methods statically generates an E_STRICT level warning.
Why static and not instantiate the object? Each programmer will tell you a reason. I particulary preffer instantiate object than use static methods. It's clear, traceable and more reusable.
I did a test bench and the difference was minimal between instantiate and call a method than call it staticaly.
Tip: if you foresee calling methods statically defines them as well;-)
首先,您无法使用 Java 等严格类型语言在您的帖子中执行此类操作。如果您在静态上下文中调用非静态内容,Java 代码将无法编译。 PHP 对这些事情还没有那么严格,但你仍然不应该仅仅因为你可以做某事,尽管这是不好的做法,在某些语言中甚至是“不正确”的做法。
使用静态方法肯定有优点。而如果你一无所获,甚至失去了灵活性,那就不太对劲了。让我们举个例子:
现在我们可以这样调用该类:
它输出
但您也可以这样做:
c()
现在执行了两次,但您明白了。在您的类中,您可以实例化类本身并像使用常规对象一样使用它。但从外部看,它只是一行代码,而使用非静态方式则是三行代码。很酷吧?正如您所看到的,您可以在非静态上下文中使用静态函数,这意味着您可以将方法声明为静态,但是如果您实例化您的类,则可以像常规方法一样简单地调用它。对我来说听起来相当灵活;)
不,你不能在静态上下文中使用
$this
,但这就是self
的用途;)First off, you couldn't do stuff like that in your post in a strict typed language like Java. Java code doesn't compile, if you call non-static stuff in a static context. PHP is not that strict on these things (yet), but still you shouldn't do things just because you can, although it's bad practice and in some languages even 'incorrect' practice.
There sure are advantages using static methods. And it's not quite right that you gain nothing or even lose flexibility. Let's have an example:
Now we can call the class this way:
which outputs
But you can do the same with:
c()
is executed twice now, but you get the idea. Within your class, you can instanciate the class itself and work with it like with a regular object. But from outside, it's simply one line of code while it's 3 lines using the non-static way. Pretty cool, huh?And as you see, you can use the static function in a non-static context, which means, you can declare your method static, but if you instanciate your class, you can simply call it like a regular method. Sounds pretty flexible to me ;)
And no, you can't use
$this
in a static context, but that's whatself
is for ;)如果它是静态函数,则不必实例化该类即可使用该方法。
VS
这里重要的是对象的实例化。实例化一个对象后,就可以对其进行操作,并且调用您的方法可能会产生意外的结果。如果您的值和函数是在类中静态声明的,则它们在调用函数时无法修改。
这并不是说您应该始终使用静态方法。以下面的例子为例。
我让 My_Math 返回一个常量值,因为我知道 pi 不会改变。但在 My_bakery 中,有时我想要蓝莓派,有时我想要桃子派。
If it is a static function you don't have to instantiate the class in order to use the method.
VS
The important thing here is the instantiation of the object. After you've instantiated an object, it can be manipulated, and calling your methods may yield unexpected results. If your values and functions are statically declared in the class, they cannot be modified at the time you call the function.
This is not to say they you should always use static methods either. take the following example.
I've made My_Math return a constant value, because I know pi doesn't change. But in My_bakery, some days I want blueberry pie and some days I want peach pie.