Jump to content

Define Data-Type in Parameter?


TomTees

Recommended Posts

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
Share on other sites

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?!  :shrug:

 

 

 

TomTees

 

 

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.