我正在研究我的书上所说的 C++ 中的“常量成员函数”。也就是说,函数不能更改类的任何属性或调用非常量的其他方法。因此,我用 C++ 编写了这段代码。
#include <iostream>
#include <string>
using namespace std;
class Foo
{
string outra_coisa;
public:
void printa_algo(string algo) const;
};
int main()
{
Foo tolo;
string alguma_coisa = "coisa_alguma";
tolo.printa_algo(alguma_coisa);
return 0;
}
void Foo::printa_algo(string algo) const
{
cout << algo;
}
是否可以在 PHP 中做同样的事情?
最佳答案
在“编译器”级别上这不是完全相同的概念,您可以创建不完全相同的静态方法,但也应该“不更改任何类属性或使用其实例”。
class Foo {
public static function bar()
{
return 'a static value';
}
}
//Then to call it:
Foo::bar();
关于php - 如何在 PHP 中创建 const 成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45341085/