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

标签 php function methods constants php-5.5

我正在研究我的书上所说的 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/

相关文章:

function - 如何为 D 中函数的引用参数创建默认值?

java - 如何在没有参数和参数的方法之间传递变量

methods - 如何获取并发方法

java - 构造函数可以像方法一样执行操作吗?

C:从函数中的循环中删除多个值的方法

php - Mysql error.sql查询语法错误

php - Mysql - 处理供应商/地点对表的最有效方式

php - 使用 PHP 显示 Python 脚本输出中的多个字符串

php - 将函数传递给 CodeIgniter 中的 View ?

c - char* ft_name(args) 是否等同于 c 中的 char *ft_name(args)?