What is Casting a Data Type in PHP?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
pankajdeoria
Forum Newbie
Posts: 15
Joined: Thu Mar 12, 2009 10:58 am
Location: Bangalore,India

What is Casting a Data Type in PHP?

Post by pankajdeoria »

Hello Friends,
I am Pankaj Gupta .Can you please tell me in detail What is Casting a Data Type in PHP :?: ? I am a new user to this forum and PHP too.

Thank You.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: What is Casting a Data Type in PHP?

Post by pickle »

Casting is using a language construct to change the type of a variable.

For example:

Code: Select all

$var = 1.223;
$var2 = (int)$var;
 
//$var2 now = 1
This doesn't necessarily work between all data types.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is Casting a Data Type in PHP?

Post by panic! »

you can cast a function or method's arguments too.

Code: Select all

 
 
 
class cat
{
 
}
 
class dog
{
 
 
}
 
function cats_only(cat $xyx)
{
 
 
}
 
$jess=new cat();
$rover=new dog();
 
cats_only($jess); // works
cats_only($rover); //epic fail
 
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: What is Casting a Data Type in PHP?

Post by Chris Corbyn »

panic! wrote:you can cast a function or method's arguments too.

Code: Select all

 
 
 
class cat
{
 
}
 
class dog
{
 
 
}
 
function cats_only(cat $xyx)
{
 
 
}
 
$jess=new cat();
$rover=new dog();
 
cats_only($jess); // works
cats_only($rover); //epic fail
 
 
That's not type-casting and is misleading. It's type-hinting.

Type-casting is taking something that's one type and making it behave like it's another. The types must be compatible though.

PHP can also only type-cast between it's own primitive types, it can't type-cast say Cat to Animal and Vice-versa if you have those classes.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: What is Casting a Data Type in PHP?

Post by josh »

Chris Corbyn wrote:PHP can also only type-cast between it's own primitive types, it can't type-cast say Cat to Animal and Vice-versa if you have those classes.
And just for the record you don't need to since PHP is dynamically typed ( I know you know that Chris but posting for OP )
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is Casting a Data Type in PHP?

Post by panic! »

Oh! My bad.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: What is Casting a Data Type in PHP?

Post by josh »

panic! wrote:Oh! My bad.
Well that wasnt towards you, I was saying about type casting not type hinting
Hadi
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 4:40 am
Location: Amman - Jordan

Re: What is Casting a Data Type in PHP?

Post by Hadi »

pleae check this link i hope it will be useful :
http://www.talkphp.com/tips-tricks/1499 ... types.html
Post Reply