2005-03-29

C > PHP

PHP sucks. Well not really, but sort of.
I'm mostly pissed off at it right now because overloading is insanely stupid.
In C, you basically just do something simple like:

int other_function(int dollars, int cents) {
return -1;
}
int my_cool_overloaded_function(int argument) {
int cents = argument;
int dollars = 0;
return otherfunction(dollars,cents);
}
int my_cool_overloaded_function(float argument) {
//the following line is probably wrong. i am rusty in C and don't quite remember how casteing works
int argument_as_an_integer = (int)argument;
int dollars = argument_as_an_integer;
int cents = argument - argument_as_an_integer;
return otherfunction(dollars,cents);
}

anyhow, that's all good (yes the code is inefficent; that's intentional).
assuming i didn't screw up, if i call my_cool_overloaded_function(1.25), the second one will be called. if i call my_cool_overloaded_function(3), the first one will be. so that's pretty easy.
in PHP4, you can't overload at all! (without explicitly calling some other intermediary function to handle the overloading)
in PHP5, you can't do any function overloading outside of a class!!! and even then you still need to write the intermediary function yourself (but at least you don't have to explicitly call it)
here's pretty much the easiest way (that i can think of) to do the same thing w/ PHP5 (i don't want to even think about how i'd do it in 4):
class php_sucks_alot {
private function my_uncool_overloaded_function_whole($bla) {
$cents = $bla;
$dollars = 0;
return php_sucks_alot::other_function($dollars,$cents);
}
private function my_uncool_overloaded_function_decimal($bla) {
//again,casting is probly wrong
$argument_as_an_integer = (int)$bla;
$dollars = $argument_as_an_integer;
$cents = $bla - $argument_as_an_integer;
return php_sucks_alot::other_function($dollars,$cents);
}
private function other_function($bla1,bla2) {
return -1;
}
function __call($function_name,$function_arguments) {
switch($function_name) {
case 'my_cool_overloaded_function':
return (is_int($function_arguments[0]))? my_uncool_overloaded_function_whole($function_arguments[0]) : my_uncool_overloaded_function_decimal($function_arguments[0]);
break;
case 'other_function':
return other_function($function_arguments[0],$function_arguments[1]);
break;
default:
die("You suck at programming. $function_name doesn't exist.");
}
}
?>

and you (well i suppose i did as well. whoops.) thought coding in php took fewer lines
to make matters worse, ALL member functions must be called thru "__call" if "__call" is defined. i illustrated this by making other_funtion a member of php_sucks_alot
for more info on this crap, see the documentation.
PHP just isn't clean, simple as that. My favorite of all time is still C++ (although i am much more fluent in PHP at the moment)

Too bad there's like no hosts out there that make using C programs easy. I guess I'm stuck with PHP. Now I'm off to write __call's for all of those classes that i thought would work (and also make a new class to hold all of those great overloaded functions)

someone please remind me to never try to write elegant code in php. it's just not meant to be.
PS: Wait a ____ second! iPaska uses PHP4!!! ARGH!!! screw this, my code is just gonna be messy.
PS: Well i could figure out how to get php5 working as a CGI program, but since that's essentially the same as getting any other C++ program (i think PHP is written in C/C++ nowadays...used to be a perl CGI thingie i think) to work that way, i might as well do it in C++
THE VERDICT: I'll use quick-n-dirty workarounds to get my thing to work for now....look at possibility of getting C to run later.

0 Comments:

Post a Comment

<< Home