TomTees Posted September 30, 2010 Share Posted September 30, 2010 Is this proper PHP... function setHeat(Building $b) { $this->hotness = $b->getInsideHeat(); } That looks more like Java to me. I thought you didn't define data-types anywhere in PHP?! TomTees Link to comment https://forums.phpfreaks.com/topic/214789-define-data-type-in-parameter/ Share on other sites More sharing options...
xyph Posted September 30, 2010 Share Posted September 30, 2010 You don't have to. Whether or not you can; I honestly have never tried, so I can't help you there Link to comment https://forums.phpfreaks.com/topic/214789-define-data-type-in-parameter/#findComment-1117447 Share on other sites More sharing options...
the182guy Posted September 30, 2010 Share Posted September 30, 2010 Yes you can now do that (as of PHP5), but you don't have to. It's called Type Hinting and as you suggest it's basically a way of making sure an argument is of the type required. The type can be any class and it also works for an array. If the variable given as the argument is not of the type required then it will produce a fatal error (which you can catch). One of the advantages of type hinting is that the programmer can quickly see what type of object or array is coming into that function or needs to be passed in, thus making the code easier to read. Link to comment https://forums.phpfreaks.com/topic/214789-define-data-type-in-parameter/#findComment-1117492 Share on other sites More sharing options...
TomTees Posted September 30, 2010 Author Share Posted September 30, 2010 Yes you can now do that (as of PHP5), but you don't have to. It's called Type Hinting and as you suggest it's basically a way of making sure an argument is of the type required. The type can be any class and it also works for an array. If the variable given as the argument is not of the type required then it will produce a fatal error (which you can catch). One of the advantages of type hinting is that the programmer can quickly see what type of object or array is coming into that function or needs to be passed in, thus making the code easier to read. Why do they call it "Type Hinting" if using it makes the type mandatory? Do you know Java or any other languages? If so, is this the same as Java does, or is it still not as good? I know that personally I've never been a fan of PHP being a "loosely-typed" language... Maybe this improves that?! TomTees Link to comment https://forums.phpfreaks.com/topic/214789-define-data-type-in-parameter/#findComment-1117573 Share on other sites More sharing options...
the182guy Posted September 30, 2010 Share Posted September 30, 2010 Yes I believe it is the same as Java does. I agree with you about the name, it probably should be called something more like 'type forcing'. Link to comment https://forums.phpfreaks.com/topic/214789-define-data-type-in-parameter/#findComment-1117582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.