Strong Typing in native PHP
http://php.webtutor.pl/index.php/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english-version/
Geeft
Ik denk niet dat ik het ga gebruiken. Indrukwekkend is het, maar toch blijft het een vieze hack.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
$y = & string("aaa");
// lets check, that $y is an object
var_dump($y);
// now we are overwritting $y variable with a scalar value of "zzz"
$y = "zzz";
// var_dump() shows us, that "zzz" is still an object, not the scalar type as in regular PHP
var_dump($y);
// the line below should raise a fatal error, because "zzz" was a scalar type (string), but it will be ok, because $y is still an object (thanks to autoboxing)
var_dump($y->toUpperCase());
?>
$y = & string("aaa");
// lets check, that $y is an object
var_dump($y);
// now we are overwritting $y variable with a scalar value of "zzz"
$y = "zzz";
// var_dump() shows us, that "zzz" is still an object, not the scalar type as in regular PHP
var_dump($y);
// the line below should raise a fatal error, because "zzz" was a scalar type (string), but it will be ok, because $y is still an object (thanks to autoboxing)
var_dump($y->toUpperCase());
?>
Geeft
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
object(String)#1 (2) {
["value"]=>
string(3) "aaa"
["ref":protected]=>
int(1)
}
object(String)#2 (2) {
["value"]=>
string(3) "zzz"
["ref":protected]=>
int(1)
}
string(3) "ZZZ"
["value"]=>
string(3) "aaa"
["ref":protected]=>
int(1)
}
object(String)#2 (2) {
["value"]=>
string(3) "zzz"
["ref":protected]=>
int(1)
}
string(3) "ZZZ"
Ik denk niet dat ik het ga gebruiken. Indrukwekkend is het, maar toch blijft het een vieze hack.
Het is niet helemaal zoals jij het hier plaatst. Je hebt nog wel een aantal classes nodig.
Maar ja, volgens mij is dit inderdaad niet echt nuttig. Autoboxing is handig bij java wanneer je een int in een Integer wilt stoppen.
Maar ja, volgens mij is dit inderdaad niet echt nuttig. Autoboxing is handig bij java wanneer je een int in een Integer wilt stoppen.
Ja natuurlijk heb je hiervoor de lib nodig die in het artikel wordt beschreven. Ik geef alleen aan hoe makkelijk het gebruik ervan is.