CodeIgniter Forums
Force type for class attribute - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Force type for class attribute (/showthread.php?tid=51059)



Force type for class attribute - El Forum - 04-19-2012

[eluser]drazik[/eluser]
Hi !
I've a problem with an object which is the result of a $query->result('User')
Actually, my User class has only public attributes. So I get an Object like :

$id => '123'
$name => 'toto'
$admin => '0'

But I want $id and $admin to be integers. I want something like :
$id => 123
$name => 'toto'
$admin => 0

Does anybody knows some technic to "transform" the attributes into integer when we are setting its value?

Thanks !


Force type for class attribute - El Forum - 04-19-2012

[eluser]InsiteFX[/eluser]
cast them with (int) $id (int) $admin



Force type for class attribute - El Forum - 04-19-2012

[eluser]drazik[/eluser]
Yep, it will work fine, but I wanted it to be automatically. Like an event when you set a value to an attribute. It seems it doesn't exist, so I think your solution is going to be the only one!


Force type for class attribute - El Forum - 04-19-2012

[eluser]boltsabre[/eluser]
>> but I wanted it to be automatically
That's not PHP. Other languages you have to declare the variable type when you create it, but not PHP. It has its advantages and disadvantages - it's pretty intuitive in it's automatic type casting, but yeah, it can cause some problems. I guess that's why they gave us the tripple equal sign operand === ;-)