> > 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php