[PHP 5.3] Language Class

Gestart doorRik18 reacties55 weergaven

19 berichten · pagina 1 van 2

Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Hoi leden!

Sinds de S3 migratie ben ik wat gaan testen met nieuwe functionaliteit van PHP5.3 en PHP 5.4. Ik heb hiermee een Language class die werkt met het overloading-principe. Dit is een manier waarbij je een method aanroept die niet bestaat, welke vervolgens door een magic mathod (__Call of __Callstatic bijvoorbeeld) opgevangen wordt.

De code

Code zonder rare { }-fratsen
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
 * @author Rik Somers
 * @copyright 2012
 */[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] L
[COLOR= #007700]{[/COLOR]    
    [COLOR= #FF8000]/**
     * An array which holds all language variables.
     */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700];[/COLOR]

    [COLOR= #FF8000]/**
     * Method overloading by using __callstatic 
     */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] __callStatic[COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strArgs[/COLOR][COLOR= #007700])
    {[/COLOR] 
        [URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]::[/COLOR][URL="http://www.php.net/getText"]getText[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strArgs[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [COLOR= #FF8000]/**
     * Load a new language array
     */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] loadLang[COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrLang[/COLOR][COLOR= #007700]){[/COLOR]    
        self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$arrLang[/COLOR][COLOR= #007700];            
    }[/COLOR]
    
    [COLOR= #FF8000]/**
     * Get the text which is called by __callstatic
     */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] [URL="http://www.php.net/getText"]getText[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$arrArgs[/COLOR][COLOR= #007700])
    {[/COLOR]
        [COLOR= #FF8000]// If our overloaded method-name exists within our language array[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/array_key_exists"]array_key_exists[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700]) ===[/COLOR] TRUE[COLOR= #007700]) {[/COLOR]
            
            [COLOR= #FF8000]// If there are any arguments[/COLOR]
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrArgs[/COLOR][COLOR= #007700])) {[/COLOR]
                [COLOR= #FF8000]// return the arguments by passing them in an formatted print[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/vsprintf"]vsprintf[/URL][COLOR= #007700]([/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$arrArgs[/COLOR][COLOR= #007700]); 
            }[/COLOR]
            [COLOR= #FF8000]// If no arguments are given, return just a normal non-formatted string[/COLOR]
            [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700]];
            }
        }[/COLOR]
        [COLOR= #FF8000]// The overloaded method-name does not exist. Throw an error[/COLOR]
        [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #DD0000]_STRING [[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$strKey[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #DD0000]] NOT FOUND_[/COLOR][COLOR= #007700];
        }             
    }    
}[/COLOR]

?>
Code met rare { }-fratsen
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
 * @author Rik Somers
 * @copyright 2012
 */[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] L
[COLOR= #007700]{[/COLOR]    
    [COLOR= #FF8000]/**
     * An array which holds all language variables.
     */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700];[/COLOR]

    [COLOR= #FF8000]/**
     * Method overloading by using __callstatic 
     */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] __callStatic[COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strArgs[/COLOR][COLOR= #007700])
    {[/COLOR] 
        [URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]::[/COLOR][URL="http://www.php.net/getText"]getText[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strArgs[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [COLOR= #FF8000]/**
     * Load a new language array
     */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] loadLang[COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrLang[/COLOR][COLOR= #007700]){[/COLOR]    
        self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$arrLang[/COLOR][COLOR= #007700];            
    }[/COLOR]
    
    [COLOR= #FF8000]/**
     * Get the text which is called by __callstatic
     */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] [URL="http://www.php.net/getText"]getText[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$arrArgs[/COLOR][COLOR= #007700])
    {[/COLOR]
        [COLOR= #FF8000]// If our overloaded method-name exists within our language array[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/array_key_exists"]array_key_exists[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700],[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700]) ===[/COLOR] TRUE[COLOR= #007700]) {[/COLOR]
            
            [COLOR= #FF8000]// If there are any arguments[/COLOR]
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrArgs[/COLOR][COLOR= #007700])) {[/COLOR]
                
                [COLOR= #FF8000]// Loop trough each argument[/COLOR]
                [COLOR= #0000BB]$tmpArray[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
                [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrArgs[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700]) {[/COLOR]
                    
                    [COLOR= #FF8000]// Match any special commands[/COLOR]
                    [URL="http://www.php.net/preg_match"]preg_match[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"/{(.*?)}(.*)/"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700]);[/COLOR]
                    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700]) {[/COLOR]
                        
                        [COLOR= #FF8000]// If multiple commands are provided, execute each of them in order[/COLOR]
                        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/stristr"]stristr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #DD0000]|[/COLOR][COLOR= #007700])){[/COLOR]
                            [COLOR= #0000BB]$explodeArr[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]|[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]]);[/COLOR]
                            [COLOR= #0000BB]$tmpStr[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]2[/COLOR][COLOR= #007700]];[/COLOR]
                            [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$explodeArr[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$manip[/COLOR][COLOR= #007700])
                            {[/COLOR]
                                [COLOR= #0000BB]$tmpStr[/COLOR] [COLOR= #007700]=[/COLOR] self[COLOR= #007700]::[/COLOR]matchReplace[COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpStr[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$manip[/COLOR][COLOR= #007700]);   
                            }[/COLOR]
                            [COLOR= #0000BB]$tmpArray[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$tmpStr[/COLOR][COLOR= #007700]; 
                        }[/COLOR]
                        [COLOR= #FF8000]// Otherwise, just execute the single special command[/COLOR]
                        [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
                            [COLOR= #0000BB]$tmpArray[/COLOR][COLOR= #007700][] =[/COLOR] self[COLOR= #007700]::[/COLOR]matchReplace[COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]2[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$tmpMatch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]]);
                        }
                    }[/COLOR]
                    [COLOR= #FF8000]// Else, just return the plain textual value[/COLOR]
                    [URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
                        [COLOR= #0000BB]$tmpArray[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700];
                    }                       
                }[/COLOR]
                [COLOR= #FF8000]// Finally, return the arguments by passing them in an formatted print[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/vsprintf"]vsprintf[/URL][COLOR= #007700]([/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$tmpArray[/COLOR][COLOR= #007700]); 
            }[/COLOR]
            [COLOR= #FF8000]// If no arguments are given, return just a normal non-formatted string[/COLOR]
            [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$langArray[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$strKey[/COLOR][COLOR= #007700]];
            }
        }[/COLOR]
        [COLOR= #FF8000]// The overloaded method-name does not exist. Throw an error[/COLOR]
        [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #DD0000]_STRING [[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$strKey[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #DD0000]] NOT FOUND_[/COLOR][COLOR= #007700];
        }             
    }[/COLOR]
    
    [COLOR= #FF8000]/**
     * Match any special commands
     */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] matchReplace[COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$manip[/COLOR][COLOR= #007700]) {[/COLOR]  
        [URL="http://www.php.net/switch"]switch[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$manip[/COLOR][COLOR= #007700]) {
            case[/COLOR] [COLOR= #DD0000]U[/COLOR]    [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/strtoupper"]strtoupper[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR] [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]L[/COLOR]    [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/strtolower"]strtolower[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR] [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]UF[/COLOR]   [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/ucfirst"]ucfirst[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR]    [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]LF[/COLOR]   [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] lcfirst[COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR]    [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]S[/COLOR]    [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/str_shuffle"]str_shuffle[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR][URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]T[/COLOR]    [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/trim"]trim[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR]       [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            case[/COLOR] [COLOR= #DD0000]R[/COLOR]    [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/strrev"]strrev[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR]     [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];            
            case[/COLOR] [COLOR= #DD0000]UW[/COLOR]   [COLOR= #007700]:[/COLOR][URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/ucwords"]ucwords[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);[/COLOR]    [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
            default     :[/COLOR][URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700];[/COLOR]             [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
        }
    }    
}[/COLOR]

?>
Hoe werkt het?

[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
 * @author Rik Somers
 * @copyright 2012
 */

// require the language class[/COLOR]
[URL="http://www.php.net/require"]require[/URL] [COLOR= #DD0000]"lang.cls.php"[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #FF8000]// create a new language array (normally you would put this in a seperate file
// you will call the method with its corresponding array-key[/COLOR]
[COLOR= #0000BB]$nl[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"fillname"[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]"Vul aub uw gebruikersnaam in."[/COLOR][COLOR= #007700],[/COLOR]
            [COLOR= #DD0000]"nameinc"[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]"De gebruikersnaam %s is niet correct"[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]// same as above, but then in english[/COLOR]
[COLOR= #0000BB]$en[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"fillname"[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]"Please fill in your username"[/COLOR][COLOR= #007700],[/COLOR]
            [COLOR= #DD0000]"nameinc"[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]"The username %s is incorrect"[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]// Load our language[/COLOR]
L[COLOR= #007700]::[/COLOR]loadLang[COLOR= #007700]([/COLOR][COLOR= #0000BB]$nl[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]// call fillname() -> $nl[fillname][/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] L[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR]fillname[COLOR= #007700]() .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]// call nameinc($parameter(s)) -> formatted version of $nl[nameinc][/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] L[COLOR= #007700]::[/COLOR]nameinc[COLOR= #007700]([/COLOR][COLOR= #DD0000]{L|UF}rik[/COLOR][COLOR= #007700]) .[/COLOR] [COLOR= #DD0000]<br /><br />[/COLOR][COLOR= #007700];[/COLOR] 

[COLOR= #FF8000]// Load a new language[/COLOR]
L[COLOR= #007700]::[/COLOR]loadLang[COLOR= #007700]([/COLOR][COLOR= #0000BB]$en[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]// same as with $nl[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] L[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR]fillname[COLOR= #007700]() .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] L[COLOR= #007700]::[/COLOR]nameinc[COLOR= #007700]([/COLOR][COLOR= #DD0000]{L|UF}rik[/COLOR][COLOR= #007700]);[/COLOR]


Wat is dan dat {L|UF}?
Ik heb voor de lol nog een extra feature ingebouwd. Hiermee kan je parameters snel aanpassen. Je moet er wel voor zorgen dat je de commands vooraan zet. Alles wat voor { } komt wordt niet meer meegenomen.

De volgende commands zijn mogelijk:
  • U - Uppercase (strtoupper())
  • L - Lowercase (strtolower())
  • UF - Uppercase first (ucfirst())
  • LF - Lowercase first (lcfirst())
  • S - Shuffle string (str_shuffle())
  • T - Trim string (trim())
  • R - Reverse string (strrev())
  • UW - Uppercase Words (ucwords())

Kan je ook meerdere parameters meegeven?
Zeer zeker! Je kan zoveel parameters meegeven als dat je zelf wilt. Door de manier waarop de __callStatic magic method word aangeroepen is dit zelfs heel simpel:

[COLOR= #0000BB]<?php
$nl[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"sentence"[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]"De %s %s de %s van de %s"[/COLOR][COLOR= #007700]);[/COLOR]

L[COLOR= #007700]::[/COLOR]loadLang[COLOR= #007700]([/COLOR][COLOR= #0000BB]$nl[/COLOR][COLOR= #007700]);[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] L[COLOR= #007700]:[/COLOR]SMILEYYYYYYYYYYYBEGIN[COLOR= #007700]:[/COLOR]sentence[COLOR= #007700]([/COLOR][COLOR= #DD0000]kat[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]krabt[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]krullen[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]trap[/COLOR][COLOR= #007700]);[/COLOR]


Mag ik dit gratis gebruiken?
Uiteraard. Je mag deze code gebruiken in alle projecten die je wil. Zolang je eventuele copyright maar laat staan. Verder mag je de code ook aanpassen en herverspreiden, zolang je de copyright maar laat staan.
De code mag ook gebruikt worden in commerciële projecten, zolang je de copyright laat staan.
Donaties zijn altijd welkom, maar zeer zeker niet verplicht.
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nice man, ik ga het zeker weten gebruiken. biggrin
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Yay. Ten minste iemand die het waardeert yummie
0
Thom

Lid

Berichten
10.654
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dit had ik echt nét nodig biggrin
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Ik snap het niet..

Language-swapper test

Vul aub uw gebruikersnaam in.
De gebruikersnaam Rik is niet correct

Please fill in your username
The username Rik is incorrect


Ik snap het doel van deze code niet?
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Kun je me uitleggen WAT je niet snapt?

De class zorgt er gewoon voor dat je gebruik kan maken van meerdere talen.
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Rik schreef:
Kun je me uitleggen WAT je niet snapt?

De class zorgt er gewoon voor dat je gebruik kan maken van meerdere talen.
Waar kan je dit script voor gebruiken? Als je in het in een andere taal wilt gebruiken kan je dat toch gewoon typen?
0
Thom

Lid

Berichten
10.654
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dylanh schreef:
Eerdere citaten tonen
Rik schreef:
Kun je me uitleggen WAT je niet snapt?
De class zorgt er gewoon voor dat je gebruik kan maken van meerdere talen.

Waar kan je dit script voor gebruiken? Als je in het in een andere taal wilt gebruiken kan je dat toch gewoon typen?
Dit kan met meerdere talen erg handig zijn, aangezien alles ook in een apart bestandje gezet kan worden en omdat je de taal makkelijk kan aanroepen.
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Dylanh schreef:
Eerdere citaten tonen
Rik schreef:
Kun je me uitleggen WAT je niet snapt?
De class zorgt er gewoon voor dat je gebruik kan maken van meerdere talen.

Waar kan je dit script voor gebruiken? Als je in het in een andere taal wilt gebruiken kan je dat toch gewoon typen?
Als je een site in meerdere talen wilt voorschotelen gebruik je dit. Je kan natuurlijk ook 2 compleet verschillende sites maken en alles gewoon typen, maar dat is onhandig. Dan moet je namelijk elke pagina 2x hebben.

Met dit script (en menig anders script overigens) kan je op een makkelijkere manier taalbestanden implementeren smile
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
$nl = array("fillname" => "Vul aub uw gebruikersnaam in.",
            "nameinc" => "De gebruikersnaam %s is niet correct");

// same as above, but then in english
$en = array("fillname" => "Please fill in your username",
            "nameinc" => "The username %s is incorrect");


Waarom staat dat er dan? Ik zie alleen die tekst en Language-swapper test?
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Dylanh schreef:
$nl = array("fillname" => "Vul aub uw gebruikersnaam in.",
            "nameinc" => "De gebruikersnaam %s is niet correct");

// same as above, but then in english
$en = array("fillname" => "Please fill in your username",
            "nameinc" => "The username %s is incorrect");
Waarom staat dat er dan? Ik zie alleen die tekst en Language-swapper test?

Omdat het een test is om te laten zien wat de class kan...
Als je het nut hiervan echt niet snapt dan heb je nog wel wat te leren in de webdevelopment.
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Nice redface
Bedankt Rik thumbsup.
0
Berichten
1.053
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nice, nice en nog eens Nice! Ik wou deze week al beginnen met het schrijven van zoiets.
Nu is het niet meer nodigyummie
Bedankt dat je dit vrijgeeft!
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ik kreeg trouwens te horen dat mensen de commands onnodig vonden. Dit kan ik er opzich ook uithalen?
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Ja ik vind dat je die commands rechtstreeks met PHP moet doen, het is leuk zo en ziet er leuk uit, maar ik snap het nut niet boven de PHP commandos.
Het zou dan nuttiger zijn dat je in je taalbestand die commands kon gebruiken, al kan ik me ook geen situatie bedenken waarin dat handig kan zijn.
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren