<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>/home/antu &#187; Felder</title>
	<atom:link href="http://www.antusblog.de/tag/felder/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.antusblog.de</link>
	<description>Linux, Programmierung und andere Dinge die mich interessieren</description>
	<lastBuildDate>Mon, 26 Oct 2009 06:00:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Elegante Parameterübergabe mit Feldern</title>
		<link>http://www.antusblog.de/2009/03/31/elegante-parameterubergabe-mit-feldern/</link>
		<comments>http://www.antusblog.de/2009/03/31/elegante-parameterubergabe-mit-feldern/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 19:34:04 +0000</pubDate>
		<dc:creator>Antu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[Felder]]></category>
		<category><![CDATA[Funktionen]]></category>
		<category><![CDATA[Parameter]]></category>
		<category><![CDATA[parse_str]]></category>

		<guid isPermaLink="false">http://antusblog.de/?p=647</guid>
		<description><![CDATA[Bei Funktionen mit vielen optionalen Parametern hat man oft das Problem, dass die Parameter, die man übergeben möchte, nicht direkt an erster Stelle stehen. Der Funktionsaufruf sieht dann meist ziemlich unübersichtlich aus, da man erst einigen Parametern den Wert NULL übergeben muss. Auch bei Funktionen mit sehr vielen Parametern verliert man beim Funktionsaufruf recht schnell [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://antusblog.de/wp-content/uploads/2009/03/parameteruebergabe.png"><img class="size-full wp-image-648 alignleft" title="Parameterübergabe mit Feldern" src="http://antusblog.de/wp-content/uploads/2009/03/parameteruebergabe.png" alt="Parameterübergabe mit Feldern" width="150" height="150" /></a>Bei Funktionen mit vielen optionalen Parametern hat man oft das Problem, dass die Parameter, die man übergeben möchte, nicht direkt an erster Stelle stehen. Der Funktionsaufruf sieht dann meist ziemlich unübersichtlich aus, da man erst einigen Parametern den Wert NULL übergeben muss. Auch bei Funktionen mit sehr vielen Parametern verliert man beim Funktionsaufruf recht schnell den Überblick.</p>
<p>Es ist möglich solche Funktionen um einiges übersichtlicher und komfortabler zu machen indem man die Parameter als Feld übergibt.<br />
<span id="more-647"></span></p>
<h2>Parameter als Feld übergeben</h2>
<p>Dazu übergibt man der Funktion als einzigen Parameter ein assoziatives Feld welches die eigentlichen Funktionsparameter als Elemente enthält. Ein Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> bild_verkleinern<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$vorgaben</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'breite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'hoehe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'seitenverhaeltnis_beibehalten'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'kantenglaettung'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$parameter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vorgaben</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parameter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Breite: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$parameter</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'breite'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Aufruf der Funktion</span>
bild_verkleinern<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'breite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hoehe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">250</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Vorgegebene Werte speichert man einfach in einem separaten Feld welches man dann mittels <a href="http://de.php.net/manual/de/function.array-merge.php" target="_blank"><code>array_merge</code></a> mit dem Parameter-Feld zusammenfügt. Die vorgegebenen Parameter werden dabei von den übergebenen Parametern überschrieben, bei Parametern die nicht übergeben wurden wird der vorgegebene Wert verwendet. Der Zugriff auf die Parameter erfolgt wie bei einem ganz normalen Feld per <code>$parameter['name']</code>. Mit Feldern lassen sich übrigens auch ganz einfach Funktionen mit beliebig vielen Parametern erstellen.</p>
<h2>Parameter als Query-String übergeben</h2>
<p>Gerade für Funktionen mit sehr vielen Parametern ist die Parameterübergabe als Query-String, wie bei manchen Template-Tags von WordPress, meiner Meinung nach gut geeignet. Als Query-String wird der Teil einer URL bezeichnet der nach dem Fragezeichen kommt und die Parameter und Werte enthält die mittels der GET-Methode übertragen werden. Das sieht dann zum Beispiel so aus:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$klein</span> <span style="color: #339933;">=</span> bild_verkleinern<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'breite=500&amp;hoehe=250'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Die übergebene Zeichenkette wird mittels der Funktion <a href="http://de3.php.net/manual/de/function.parse-str.php" target="_blank"><code>parse_str</code></a> analysiert und die Parameter und Werte werden in einem Feld gespeichert. Ist <a href="http://de3.php.net/manual/de/info.configuration.php#ini.magic-quotes-gpc" target="_blank"><code>magic_quotes_gpc</code></a> aktiv werden dabei alle <code>'</code>, <code>"</code>, <code>\</code> und <code>NUL</code>-Zeichen mit einem Backslash geschützt, da man das normalerweise nicht will muss man das mit <a href="http://de3.php.net/manual/de/function.stripslashes.php" target="_blank"><code>stripslashes</code></a> rückgängig machen. Anschließend wird das Feld wie oben wieder mit dem Vorgaben-Feld zusammengeführt und anschließend kann es auch genau so benutzt werden.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// stripslashes_deep arbeitet sich durch das Feld und</span>
<span style="color: #666666; font-style: italic;">// wendet auf jedes Element die Funktion stripslashes an.</span>
<span style="color: #000000; font-weight: bold;">function</span> stripslashes_deep<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wert</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$wert</span> <span style="color: #339933;">=</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wert</span><span style="color: #009900;">&#41;</span> ?
            <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stripslashes_deep'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wert</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wert</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$wert</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> parameter_parsen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vorgaben</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vorgaben</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$ergebnis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vorgaben</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span>
        <span style="color: #000088;">$ergebnis</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$ergebnis</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ergebnis</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parameter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$geparst</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$geparst</span> <span style="color: #339933;">=</span> stripslashes_deep<span style="color: #009900;">&#40;</span><span style="color: #000088;">$geparst</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$ergebnis</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ergebnis</span><span style="color: #339933;">,</span> <span style="color: #000088;">$geparst</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$ergebnis</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Beispielfunktion:</span>
<span style="color: #000000; font-weight: bold;">function</span> bild_verkleinern<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$vorgaben</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'breite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'hoehe'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'seitenverhaeltnis_beibehalten'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'kantenglaettung'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> parameter_parsen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vorgaben</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Breite: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'breite'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Diese Methode ist allerdings nicht für alle Arten von Funktionen bzw. Parametern geeignet. Ein Problem ist zum Beispiel das bestimmte Zeichen nicht mehr übergeben werden können (z.B. &amp;). Auch die Übergabe von Feldern gestaltet sich eher schwierig (grundsätzlich ist es aber möglich: <code>funktion("feld[]=hallo&#038;feld[]=welt");</code>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.antusblog.de/2009/03/31/elegante-parameterubergabe-mit-feldern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

