Versio

Strong Typing in native PHP

Overzicht Reageren

Pim -

Pim -

15/04/2011 12:01:08
Quote Anchor link
http://php.webtutor.pl/index.php/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english-version/

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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());
?>

Geeft
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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"

Ik denk niet dat ik het ga gebruiken. Indrukwekkend is het, maar toch blijft het een vieze hack.
 
PHP hulp

PHP hulp

25/05/2012 18:44:10
Gesponsorde koppelingen:
BHosted Hosting al vanaf € 1,- per maand

Controleer nu gratis jouw domeinnaam:

  
 
Karl Karl

Karl Karl

15/04/2011 14:26:37
Quote Anchor link
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.
 
Pim -

Pim -

15/04/2011 15:07:22
Quote Anchor link
Ja natuurlijk heb je hiervoor de lib nodig die in het artikel wordt beschreven. Ik geef alleen aan hoe makkelijk het gebruik ervan is.
 



Overzicht Reageren