Welcome Guest, Not a member yet? Register   Sign In
Force type for class attribute
#1

[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 !
#2

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

[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!
#4

[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 === ;-)




Theme © iAndrew 2016 - Forum software by © MyBB