# Const member functions

- Thread: https://externals.io/message/25650
- Original archive: https://news-web.php.net/php.internals/25650
- Started by tslettebo@broadpark.no on 2006-09-13T07:48:49+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #25650 — tslettebo@broadpark.no — 2006-09-13T07:48:49+00:00

URL: https://externals.io/message/25650#25650

(This may be considered too radical for some, but I ask, anyway... Also, if
there's a more appropropriate place to ask such questions, let me know, but
as this is the developer's list, it seemed like the right place)

In C++, it's possible to declare member functions "const" meaning they don't
change the object they operate on. This can help reason about programs,
because if you have something like (PHP syntax):

function some_member()
{
   ...
   $a = $this->f();
   $b = $this->g();
   $c = $this->h();
   ...
}

and f(), g() and h() are all declared "const", you know the object is still
in the same state as before the functions were called. I was recently "bit"
by this, when I changed the state in a member function, and later called
that member function, thinking it _didn't_ change the state, and wasted some
time debugging that. Had it been possible to declare your assumption (this
function doesn't change the object), I'd got an error where it did change
the object, clearly showing the erroneous assumption.

Thoughts?

As I'm pretty ignorant about the internals of PHP, I don't know if this is
practical to implement, or whether it might "fit" with the language.

It's possible to _simulate_ this effekt to some degree, but at the cost of
both syntactic noise and performance: In short, it's a hack. It involves
creating an object at the start of the function definition, taking a
reference to $this, and comparing the new state with a copy of it, in the
destructor (i.e. at the end of the function). However, it's a rather
inelegant "solution", with a potentially large performance impact.

Regards,

Terje

---

## #25669 — Stanislav Malyshev — 2006-09-13T18:04:45+00:00

URL: https://externals.io/message/25650#25669 · In reply to: #25650

> In C++, it's possible to declare member functions "const" meaning they don't
> change the object they operate on. This can help reason about programs,
> because if you have something like (PHP syntax):

It would be pretty hard to enforce in PHP - how do you know the object 
is not changed, without strictly typing all the functions that access 
it? Also, while in C++ compiler can benefit from knowing the function is 
const - i.e. from knowing nothing was changed by it regarding this 
argument - PHP engine hardly can do it.

---

## #25674 — tslettebo@broadpark.no — 2006-09-13T18:52:13+00:00

URL: https://externals.io/message/25650#25674 · In reply to: #25669

> > […]
don't
> > […]
>
> It would be pretty hard to enforce in PHP - how do you know the object
> is not changed, without strictly typing all the functions that access
> it?

Like I've mentioned, I don't know how PHP does this internally, so maybe
this is hard to detect (or may result in nontrivial overhead). I'm not sure
what you mean by "strictly typing all the functions that access it": The
modification detection would likely have to happen at run-time (not
compile-time), due to PHP's dynamically typed nature, so I can't see what
typing would have to do with it.

> Also, while in C++ compiler can benefit from knowing the function is
> const - i.e. from knowing nothing was changed by it regarding this
> argument - PHP engine hardly can do it.

Much, if not almost all, of the benefit of "const" is about program
correctness and easier reasoning about programs (if you know something is
const, including variables, you know you can rely on them not having been
changed, even a zillion lines below where they were defined), i.e.
advantages for the _developer_, foremost. It _might_ give some possibilities
of optimisation, in a statically typed language, but that's more of a side
effect.

Regards,

Terje
