Posted by Stas on March 31, 2007
Not exactly what you thought reading the title, sorry
Just wanted to write about the topic discussed elsewhere – how one could do money calculations with PHP? PHP has no BCD type and no arbitrary precision float type either. And for money calculations is it important to have it very precise – accountants can not allow even single penny to slip by (remember the plot of the Office Space movie? 
So, using regular floats/doubles is not good in this case – for starters, there’s no precise representation for number as simple as 0.1! So if you make a lot of calculations with such numbers errors would creep in. Now the question is what could be done about it?
One solution is to make all calculations in units like one hundredth or thousandth of a cent and make everything integer, using arbitrary-precision integers like in GMP and then when printing it just shift the point and cut the extra digit whenever needed. This might be good enough if we knew how precise we should get.
Even better would be to have a type that combines arbitrary-precision integer and decimal point position, like Java does. I wonder if something can be done with GMP. It does have arbitrary precision floats, but I’m not sure it would fit.
Or, maybe there’s some other, better solutions for this? I don’t have much experience with money calculations and problems and solutions in that area…
Posted in Functions | 11 Comments »
Posted by Stas on March 23, 2007
As everybody knows, one of very nice OO features PHP 5 has is – if method that is not defined is called on an object of a class, the class could define catch-all method named __call and thus route this method call in any way the developer wants, transparent to the user. This allows very flexible way of defining interfaces between classes – even between entities that their interface might be not known to the developer of the class, such as SOAP services. Very useful indeed.
However, we can not do this on a class itself – we couldn’t define static __call and have it route class (static) method calls the same way regular __call routes the object method calls. I wonder maybe we should have it. Along with all other __methods for overloading stuff, of course. We couldn’t probably name it __call since we already have one call but something like __scall could work.
Posted in Engine, Functions | 11 Comments »
Posted by Stas on March 21, 2007
This expression – dirname(__FILE__) – is used in a real lot of places. The reason is simple – libraries want to include files relative to library top directory, and do not want to count on include path. And relative include resolution rules in PHP not clear to all, so people prefer to be sure. The downside here is that this expression is dynamic – executed at run-time. Meaning it’s slower and less toolable and also makes a bad habit of putting dynamic things into include (which is not a problem here, since it’s “static dynamic” thing, but still a bad habit).
So why won’t we have constant that would mean dirname(__FILE__)? Something like __FILEDIR__. Would make a lot of code cleaner.
The problem with it of course to make real use in code we should travel back in time to 1996 and add it there
Posted in Functions | 20 Comments »