Monday, 26 August 2013

PHP Type Hinting - Is it truly pointless?

PHP Type Hinting - Is it truly pointless?

Is there any legitimate use for PHP type hinting? Does it improve
execution time or something?
I ask because you cannot overload by type, so using it as a means to avoid
an if statement are gone, for example:
class Whatever {
public function doSomething(SomeObject $with) {}
public function doSomething(SomeOtherObject $with) {}
}
IS NOT valid, it takes that as re-declaring a function, you also can't
specify a return type, so that means this is a runtime thing! It couldn't
be done statically, also duck-typing and static-typing mix like oil and
water (I pondered the idea a while back, it's lead to me falling in love
with compilers and methods of parsing)
The only use I have found for it is IDE auto-completion.
BUT you cannot use PHPs "native" (for lack of a better word) data
'structures' (if array can be called a structure, I like lists, I like
maps, I don't like mutant hybrid things) for type hinting, you cannot
overload == or === so this lead me to create "Object" and have a whole
bunch of things that derive from it, when you do this it is scary how much
PHP looks like Java.
I don't really like PHP and there are a lot of things I hate about it
(gettype and get_class, or is the underscore the other way around? The not
transitive-ness of ==, how you can have __toString but not __toBool, and
stuff....)
BUT back on topic, is type-hinting of ANY USE at all, other than for IDE
hinting, which given the problems I am willing to sacrifice.
BONUS QUESTION: I use Eclipse, with pydev if you use "isinstance" and
assert on the same line the IDE learns the type of the variable, this is
good. Eclipse and whatever it's PHP plugin is called does not do this,
Typehinting is the only way (unless you used new in the scope or
something) I believe, my question is, is there a way to get eclipse to
follow the types better? Through some sort of comment explaining variable
types or something.....
Thanks
PS: I've read PHPs docs on the matter.
http://php.net/manual/en/language.oop5.typehinting.php Proof! Okay as it
turns out you can use PHPs arrays a type hint, point still stands though.
PPS: PHP treats function definitions as the same (ALSO: it's case
insensitive for function names only <-- GRR) because of that function with
underscores that gets function arguments, it could be get_function_args,
get_func_arguments, it's PHP, I'd have to search, it's usually the first
result but still, I'd have to search, anyway, because of that, redefining
functions makes no sense, number of arguments doesn't distinguish
functions because they may use this argument returning function, PHP
doesn't do the static analysis to read ahead (because it allows things
like $$a a variable with the name of whatever variable $a is, static
analysis isn't always possible) so it can't whine but let you overload
most of the time 'cept when you use this get_function_args or whatever it
is.
Type hinting seems totally pointless, not only pointless but limiting, I
cannot see any reason why I'd use it, I only use PHP because I have to
because of such silly things, and my love of auto complete made me wreck
my code, I am now trying to backtrack, I use version control though (we
all should) so if I've missed something super-dooper, tell me and I shall
revert!

No comments:

Post a Comment