(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
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.
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?
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