问题描述
好的,我正在学习这段代码
ok I have this code, that I'm studying
class scope{
function printme(){
return "hello";
}
public static function printme(){
return "hello";
}
}
$s = new scope();
echo $s->printme(); //non-static call
echo "<br>";
echo scope::printme(); //static call
现在,这实际上不是我项目的代码,但这是我想要做的事情
Now, this is not really the code of my project but these are the things I want to do
- 我想创建一个包含静态和非静态函数的类.
- 我希望在静态和非静态调用中都可以使用该功能.
由于非静态函数具有很多操作,因此我还需要将其称为静态函数,这样就无需实例化该类.这可能吗?还是真的需要将该函数重写为另一个函数或类?
As non-static function has a lot of operations on it, I also need to call it as a static function so that I will not need to instantiate the class. Is this possible? or I really needed to rewrite the function to another function or class?
注意:告诉我我是否已经在做一些不好的编程了.
NOTE: tell me if I'm doing some bad programming already.
推荐答案
以下是规则:
Here is the rule:
静态方法可以用于静态方法,也可以用于非静态方法.
A static method can be used in both static method and non-static method.
非静态方法只能在非静态方法中使用.
A non-static method can only be used in a non-static method.
这篇关于PHP中的静态和非静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!