phpfunctionmethodsconstantsphp-5.5

How to make const member-functions in PHP?


I was studying what my book called "constant member functions" in C++. That is, functions that can not change any attribute of the class or call other methods that are not constants. So, I made this code in 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;
}

Is it possible to do the same in PHP?


Solution

  • Well after some research I saw in a book that it is not possible, at least is not trivial, to create such functions in PHP.