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.

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.