alexweber15 Posted September 23, 2008 Share Posted September 23, 2008 if you expect a parameter to be of a certain type is there any benefit in using type hinting vs instanceof? class foo{ #ex1 function parseValues(ObjectType $a){ #call ObjectType method on $a } #ex2 function parseValues($a){ if($a instanceof ObjectType){ #call ObjectType method on $a }else{ return false; #or any other kind of error message } } } the way i see it whereas type hinting might be useful for documentation purposes as it makes it more obvious what kind of type you expect the passed parameter to be, the fact that it generates a fatal error = less flexibility. in ex2 you have the same functionality but by using isntanceof you get the same security that the method you want to call will be there and it also allows you to return an error message of some sort without generating a fatal error. so in a nutshell: is there any benefit of using Type Hinting as opposed to instanceof? (afaik Type Hinting also implies that the comparison will be made implicitly; ie the 'performance hit' of using the conditional statement is the same in both cases) Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/ Share on other sites More sharing options...
aschk Posted September 25, 2008 Share Posted September 25, 2008 Personally type hinting is better, it allows for interfaces/abstract classes to be implemented with stricter controls, and will error early giving you advanced warning of production code that might not function. Also, if you need to create 20 classes and each one has a "instanceof" check in it, are you 100% sure you're going to right the if/else statement and handle the errors correctly each time? If it doesn't conform to the type hint then you really don't want your code to function at all, because it's usually an object dependency that'll have knock-on affects. Just my 2 pence. Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-650426 Share on other sites More sharing options...
alexweber15 Posted September 25, 2008 Author Share Posted September 25, 2008 Personally type hinting is better, it allows for interfaces/abstract classes to be implemented with stricter controls, and will error early giving you advanced warning of production code that might not function. Also, if you need to create 20 classes and each one has a "instanceof" check in it, are you 100% sure you're going to right the if/else statement and handle the errors correctly each time? If it doesn't conform to the type hint then you really don't want your code to function at all, because it's usually an object dependency that'll have knock-on affects. Just my 2 pence. fair enough! i personally prefer type-hinting too but I've had one or another situation where an object failed to get properly instantiated and then i got a fatal error but i guess that's just PHP for "create your damn object properly next time" Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-650431 Share on other sites More sharing options...
corbin Posted September 26, 2008 Share Posted September 26, 2008 "the way i see it whereas type hinting might be useful for documentation purposes as it makes it more obvious what kind of type you expect the passed parameter to be, the fact that it generates a fatal error = less flexibility." Less flexibility is good sometimes. If you're expecting an object a with methods B and you get an object b with methods C such that C != B, you have some major problems if you try to use a method from B that does not exist in C. Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651495 Share on other sites More sharing options...
alexweber15 Posted September 26, 2008 Author Share Posted September 26, 2008 "the way i see it whereas type hinting might be useful for documentation purposes as it makes it more obvious what kind of type you expect the passed parameter to be, the fact that it generates a fatal error = less flexibility." Less flexibility is good sometimes. If you're expecting an object a with methods B and you get an object b with methods C such that C != B, you have some major problems if you try to use a method from B that does not exist in C. true, but with instanceof you could also avoid the fatal error and throw an exception instead for example Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651516 Share on other sites More sharing options...
corbin Posted September 26, 2008 Share Posted September 26, 2008 If you want to get technical, with a custom error handler you could essentially do the same thing ;p. Hrmmm, guess each would have its advantages. Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651578 Share on other sites More sharing options...
alexweber15 Posted September 26, 2008 Author Share Posted September 26, 2008 true enough i avoid using Exceptions unless i really have to... if statements might be less elegant but are definitely more efficient but i guess it comes down to personal preference in the end Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651590 Share on other sites More sharing options...
corbin Posted September 26, 2008 Share Posted September 26, 2008 I personally use exception [the way I'm understood they're supposed to be used] only in the case that something out of the ordinary happens. For example, if a DB connection fails I would throw an exception. On the other hand, if user input didn't validate, I wouldn't since that is expected. Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651606 Share on other sites More sharing options...
alexweber15 Posted September 27, 2008 Author Share Posted September 27, 2008 I personally use exception [the way I'm understood they're supposed to be used] only in the case that something out of the ordinary happens. For example, if a DB connection fails I would throw an exception. On the other hand, if user input didn't validate, I wouldn't since that is expected. you're right as far as the theoretical part goes... Exceptions should in fact be used! I just feel that they're not as polished as in other OO languages so far but from what I've read in PHP6 they should be better. It's also good practice to get into it right now so that when they do become less "bulky" you don't have to play any catch-up Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651697 Share on other sites More sharing options...
corbin Posted September 27, 2008 Share Posted September 27, 2008 I hardly ever use exceptions besides DB stuff.... hehe Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651708 Share on other sites More sharing options...
alexweber15 Posted September 27, 2008 Author Share Posted September 27, 2008 I hardly ever use exceptions besides DB stuff.... hehe lol same here! Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651715 Share on other sites More sharing options...
Daniel0 Posted September 27, 2008 Share Posted September 27, 2008 I use exceptions all the time... if (!$this->acl->isAllowed('current_user', 'content', 'read')) { throw new PHPFreaks_Exception('Access denied. You do not have sufficient permissions to view this page.'); } Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651755 Share on other sites More sharing options...
alexweber15 Posted September 27, 2008 Author Share Posted September 27, 2008 Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-651961 Share on other sites More sharing options...
corbin Posted September 28, 2008 Share Posted September 28, 2008 Hrmm I guess an exception would actually make a lot of sense there, but I would probably stick with my if-else habbit ;p. Quote Link to comment https://forums.phpfreaks.com/topic/125484-solved-type-hinting-vs-instanceof/#findComment-652182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.