Page 1 of 1
PHP types
Posted: Fri Jul 28, 2006 1:02 am
by alex.barylski
I have seen some code examples on here which almost appear to be declaring the data type of function parameters...
WTF is this all about???
Does PHP support phantom types or something??? Is this so you *can* explicitly declare types? Does this only work in function arguments???
What is the practical purpose behind this is such a loosely typed language???
Clearly for aesthetics or code clarity?
I'm curious...
Cheers

Re: PHP types
Posted: Fri Jul 28, 2006 1:07 am
by feyd
Hockey wrote:I have seen some code examples on here which almost appear to be declaring the data type of function parameters...
WTF is this all about???
type hinting.
Hockey wrote:Does PHP support phantom types or something???
array and object type hinting is supported (5+, 5.1+ for arrays)
Hockey wrote:Is this so you *can* explicitly declare types?
yes
Hockey wrote:Does this only work in function arguments???
yes
Hockey wrote:What is the practical purpose behind this is such a loosely typed language???
making more strict interfaces, having php take care of a lot of checking you'd have to do otherwise, at compile time.
Hockey wrote:Clearly for aesthetics or code clarity?
try it, find out.
Posted: Fri Jul 28, 2006 1:23 am
by alex.barylski
Hmmm...so it's optional?
I have PHP 5 on my local server...and run non-typed code all the time...hasn't choked, so I assume it is???
Nice idea I suppose...the parser chokes then if I pass a type
string to a argument which is of type
array?
I like the idea...as I prefer working with strict types...just confused the *(&#$^& because I've become so accustomed to not caring about types in PHP...
Figured it was people coming from C++ just making their own code more explicit...in forums, etc...
Coolness...I'll have to start using that...
Thanks

Posted: Fri Jul 28, 2006 1:28 am
by daedalus__
Posted: Fri Jul 28, 2006 1:36 am
by Luke
As a result of the thread dae just posted, I have begun doing this too... it keeps me disciplined... by that I mean it prevents me from doing stupid things with my code.
Posted: Fri Jul 28, 2006 1:41 am
by daedalus__
If you read through the whole thread, if you are as stupid as I am, you learned that type hinting only works for two things:
objects and arrays
When you are 'hinting' at an object type, you have to specify the type (class) of the object as well.
I don't know about anyone else but I was disappointed.

Posted: Fri Jul 28, 2006 1:47 am
by feyd
I would guess the reason why there are no string/int/bool type hints is because they are scalar to php, therefore interchangable.
Posted: Fri Jul 28, 2006 1:49 am
by Luke
yes I could really give a crap about that anyway... but making sure it is of the type "User" and not "HTTP_Request" is very nice.
Posted: Fri Jul 28, 2006 1:49 am
by daedalus__
when you hit a word that does type hinting in dreamweaver it highlights it some hard to read color and i remembered the reason that I thought it supported those other types was because it highlighted them the same ugly teal color as object. if it doesn't support those types then i wonder why it highlighted them ??
Posted: Fri Jul 28, 2006 1:52 am
by feyd
likely, because they are valid type-casting keywords.