PHP Typecasting - Good or bad?

marquitavaldez

New Member
After some work in C and Java I've been more and more annoyed by the wild west laws in PHP. What I really feel that PHP lacks is strict data types. The fact that string('0') == (int)0 == (boolean)false is one example.You cannot rely on what the data type a function returns is. You can neither force arguments of a function to be of a specific type, which might lead to a non strict compare resulting in something unexpected. Everything can be taken care of, but it still opens up for unexpected bugs.Is it good or bad practice to typecast arguments received for a method? And is it good to typecast the return?IE\[code\]public function doo($foo, $bar) { $foo = (int)$foo; $bar = (float)$bar; $result = $bar + $foo; return (array)$result;}\[/code\]The example is quite stupid and I haven't tested it, but I think everyone gets the idea. Is there any reason for the PHP-god to convert data type as he wants, beside letting people that don't know of data types use PHP?
 
Back
Top