如何在 PHP 中创建 const 成员函数?

How to make const member-functions in PHP?

我正在研究我在 C++ 中称为 "constant member functions" 的书。也就是说,不能更改 class 的任何属性或调用其他非常量方法的函数。所以,我用 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 中执行相同的

经过一些研究,我在一本书中看到,在 PHP 中创建这样的函数是不可能的,至少不是微不足道的。

在 "compiler" 级别上这不是完全相同的概念您可以创建不完全相同但也应该 "not change any class property or use its instance".

的静态方法
class Foo {
    public static function bar()
    {
        return 'a static value';
    }
}
//Then to call it:
Foo::bar();