Re: Inline typecasting / typehinting for classes and interfaces

From: Date: Wed, 15 Aug 2012 09:17:46 +0000
Subject: Re: Inline typecasting / typehinting for classes and interfaces
References: 1 2 3 4 5 6 7 8 9 10 11 12  Groups: php.internals 
Request: Send a blank email to internals+get-62175@lists.php.net to get a copy of this message
Hi!
All right, your method accepts an array of objects, instances of Foo. How do you enforce this contract?
You are trying to re-invent generics/parametrized types and bolt it onto PHP. I still don't think it is a good idea - PHP is not a statically typed language. Just check you array elements if you need.
You're 10 years too late to argue the merits of typehints in PHP. I am not proposing the introduction of typehints. I'm just saying "we have the option to use them in arguments, let's have the options inline too." This way we can validate expressions and structures we can't today, in a minimal, readable, consistent, easy to maintain way.
By "peppering" here you mean one if. Yes, this is my proposal - if you need to do a check - do a check.
Yes, exactly: one if. For your entire codebase that has one single assignment in it. Or one foreach. Pick one or the other, but not both, otherwise they become two ifs, and that invalidated your argument.
The same applies to typehints, so make up your mind.
No it does not. But this is exactly why I think one of the problems with strict typing in arguments is - because people take it and starting to use it as a legitimation of turning PHP into weird hybrid of dynamically typed language with random islands of static typing popping in random places. I still do not think it is a good idea.
Saying "weird hybrid random islands popping random" doesn't make simple type validation hints any more awkward than they were yesterday before I posted this thread, Stas. Stan

« previous php.internals (#62175) next »