Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/entry.php on line 11 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/entry.php on line 12 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/entry.php on line 16 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/entry.php on line 24 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/includes/header.inc.php on line 16 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/includes/header.inc.php on line 23 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Core.class.php on line 86 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Core.class.php on line 87 Deprecated: Function set_magic_quotes_runtime() is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/includes/core.inc.php on line 14 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/UrlManager.class.php on line 39 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/UrlManager.class.php on line 68 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/UrlManager.class.php on line 93 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Page.class.php on line 38 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Page.class.php on line 261 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Page.class.php on line 265 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/AcronymFinder.class.php on line 26 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/AcronymFinder.class.php on line 36 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/DynamicTags.class.php on line 30 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/DynamicTags.class.php on line 94 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Category.class.php on line 29 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Category.class.php on line 155 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Category.class.php on line 173 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Category.class.php on line 190 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php on line 267 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php on line 292 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php on line 316 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php on line 349 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php on line 434 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Comment.class.php on line 37 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Comment.class.php on line 238 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Comment.class.php on line 252 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Pingback.class.php on line 22 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Pingback.class.php on line 84 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Archive.class.php on line 27 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Archive.class.php on line 103 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Archive.class.php on line 152 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Archive.class.php on line 173 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Archive.class.php on line 209 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Blogmark.class.php on line 21 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Login.class.php on line 18 Deprecated: Assigning the return value of new by reference is deprecated in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Login.class.php on line 89 Warning: Cannot modify header information - headers already sent by (output started at /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Entry.class.php:267) in /storage/content/41/171341/se.samuelsjoberg.com/public_html/includes/header.inc.php on line 13 Warning: get_class() expects parameter 1 to be object, array given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Warning: get_class() expects parameter 1 to be object, array given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Warning: get_class() expects parameter 1 to be object, array given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Warning: get_class() expects parameter 1 to be object, array given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Warning: get_class() expects parameter 1 to be object, integer given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Warning: get_class() expects parameter 1 to be object, string given in /storage/content/41/171341/se.samuelsjoberg.com/public_html/classes/Template.class.php on line 40 Iteratorer i PHP 5
se.samuelsjoberg.com

Iteratorer i PHP 5

Från början var tanken att denna blogg skulle snurra på PHP 5. Jag hade hunnit så långt att allt var klart och fungerade lokalt. Det hela brast när det visar sig att mitt webbhotell inte kan uppgradera mig till PHP 5 på grund av den server jag ligger på.

Det slutade med att jag fick använda den gamla blogmotorn som körs på PHP 4. Trist, men inget som gör någon skillnad varken för mig som skribent eller dig som besökare. PHP 5 lösningen var bara så mycket trevligare rent tekniskt. En komplett MVC lösning där jag drog nytta av nyheterna i version 5 som t.ex. interface, encapsulering och nya magiska __set() och __get().

Men en av de kanske trevligaste överraskningarna var att man äntligen kunde implementera iteratorer i PHP. Ett objekt som implementerar Iterator kan man loopa över med en foreach. Det första användningsområde för iteratorer som jag såg var hanteringen av databasresultat. Klassen DBResult är ett exempel på ett objekt som tar ett result set (mysql) och gör det lite trevligare att jobba med.

Och så slutligen. Det kanske trevligaste med iteratorer. Om jag exempelvis har ett arkiv (EntryArchive) som skall returnera ett antal inlägg så kan jag göra detta on-the-fly med en iterator (som i sin tur delegerar till stora delar till DBResult) istället för att först bygga upp en array av objekt. Inlägget kommer alltså inte att skapas förrän man loopar över arkivet (t.ex. vid utskrift). Toktrevligt.

Om bara webbhotellet hade stöd för PHP 5...


Kommentera inlägget

Kommentaren kunde inte sparas: