PHP types

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

PHP types

Post by alex.barylski »

I have seen some code examples on here which almost appear to be declaring the data type of function parameters...

Code: Select all

function my_func(string $string);
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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: PHP types

Post by feyd »

Hockey wrote:I have seen some code examples on here which almost appear to be declaring the data type of function parameters...

Code: Select all

function my_func(string $string);
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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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 ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

likely, because they are valid type-casting keywords.
Post Reply