Jump to content

[SOLVED] type hinting vs instanceof?


alexweber15

Recommended Posts

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"  :D

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

"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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.