Re: Inline typecasting / typehinting for classes and interfaces

From: Date: Wed, 15 Aug 2012 08:43:13 +0000
Subject: Re: Inline typecasting / typehinting for classes and interfaces
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-62173@lists.php.net to get a copy of this message
Hi!
Let me ask you - do you think the existing PHP typehints are pointless too? Do you feel they don't give you enough flexibility? Do you feel they reinvented a language construct for the purpose of saving the typing of one if clause (per argument) (per method) (per class)?
They are not pointless, but I think they are often misunderstood and not used correctly. And they definitely lack flexibility in many cases. But they are helpful for one important thing - defining interface between the method and the method client. Having strictly typed variables does not serve it, and trying to make PHP into half-baked half-statically-typed language does not sound like a good idea to me.
All right, your method accepts an array of objects, instances of Foo. How do you enforce this contract? Here's how my proposal enforces it: function foobar(array $collectionOfFoo) { foreach ($collectionOfFoo as Foo $item) {
       ...
} } What's your proposal? From the discussion so far, I'd guess it's peppering out code with if-s with the same error written everywhere everytime we check the item in an array.
Because I don't think inventing language constructs for the purpose of helping IDEs simulate static typing in dynamically typed language makes much sense.
The same applies to typehints, so make up your mind. Stan

« previous php.internals (#62173) next »