<?xml 
version="1.0" encoding="utf-8"?>
<rss version="2.0" 
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
>

<channel xml:lang="fr">
	<title>6tech, la solution webcreation</title>
	<link>http://6tech.net/</link>
	
	<language>fr</language>
	<generator>SPIP - www.spip.net</generator>




<item xml:lang="fr">
		<title>Paradigme des fonctions</title>
		<link>http://www.6tech.net/Paradigme-des-fonctions.html</link>
		<guid isPermaLink="true">http://www.6tech.net/Paradigme-des-fonctions.html</guid>
		<dc:date>2008-04-08T21:28:16Z</dc:date>
		<dc:format>text/html</dc:format>
		<dc:language>fr</dc:language>
		<dc:creator>Yannick Domenjoud</dc:creator>


		<dc:subject>PHP</dc:subject>
		<dc:subject>Fonctions</dc:subject>

		<description>Ceci est un post &#224; vues p&#233;dagogiques sur la cr&#233;ation de fonctions php. Tout le monde utilise les fonctions de php telles que explode(), strpos(), mysql_db_query(), etc . Mais qui n'a jamais voulu en cr&#233;er lui m&#234;me ? la cr&#233;ation/utilisation de fonctions suit 2 &#233;tapes : Tout d'abord, commen&#231;ons par le commencement : la d&#233;claration Elle ( la d&#233;claration [1]) commence par le mot-cl&#233; : function et ce, selon la syntaxe suivante : function NomDeLaFonction(arg1,arg2,arg3 .... ) corps de la (...)

-
&lt;a href="http://www.6tech.net/-Tutoriaux-.html" rel="directory"&gt;Tutoriaux&lt;/a&gt;

/ 
&lt;a href="http://www.6tech.net/+-PHP-+.html" rel="tag"&gt;PHP&lt;/a&gt;, 
&lt;a href="http://www.6tech.net/+-Fonctions-+.html" rel="tag"&gt;Fonctions&lt;/a&gt;

		</description>


 <content:encoded>&lt;div class='rss_texte'&gt;&lt;span class='csfoo somm'&gt;&lt;/span&gt;&lt;div class=&quot;cs_sommaire cs_sommaire_avec_fond&quot; id=&quot;outil_sommaire&quot;&gt; &lt;div class=&quot;cs_sommaire_inner&quot;&gt; &lt;div class=&quot;cs_sommaire_titre_avec_fond&quot;&gt; Sommaire &lt;/div&gt; &lt;ul&gt; &lt;li&gt;&lt;a title=&quot;la d&#233;claration&quot; href=&quot;http://www.6tech.net/spip.php?page=backend#outil_sommaire_0&quot;&gt;la d&#233;claration&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;L&amp;#39;appel de la fonction&quot; href=&quot;http://www.6tech.net/spip.php?page=backend#outil_sommaire_1&quot;&gt;L'appel de la fonction&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt;&lt;span class='csfoo somm'&gt;&lt;/span&gt;&lt;p&gt;Ceci est un post &#224; vues p&#233;dagogiques sur la cr&#233;ation de fonctions php.&lt;/p&gt; &lt;p&gt;Tout le monde utilise les fonctions de php telles que &lt;i&gt;explode()&lt;/i&gt;, &lt;i&gt;strpos()&lt;/i&gt;, &lt;i&gt;mysql_db_query()&lt;/i&gt;, etc .
Mais qui n'a jamais voulu en cr&#233;er lui m&#234;me ? la cr&#233;ation/utilisation de fonctions suit 2 &#233;tapes :&lt;/p&gt; &lt;p&gt;Tout d'abord, commen&#231;ons par le commencement :&lt;/p&gt; &lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_0&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;la d&#233;claration&lt;/h3&gt; &lt;p&gt;Elle ( la d&#233;claration&lt;span class='csfoo htmla'&gt;&lt;/span&gt; [&lt;a href='#nb1' class='spip_note' rel='footnote' title='doit se faire obligatoirement avant l'appel de la fonction' id='nh1'&gt;1&lt;/a&gt;]&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;) commence par le mot-cl&#233; : &lt;i&gt;function&lt;/i&gt; et ce, selon la syntaxe suivante :&lt;/p&gt; &lt;div style='text-align: left;' class='spip_code' dir='ltr'&gt;&lt;code&gt; function NomDeLaFonction(arg1,arg2,arg3 .... ){&lt;br /&gt; corps de la fonction;&lt;br /&gt; }&lt;/code&gt;&lt;/div&gt; &lt;p&gt;La fonction peut tr&#232;s bien ne prendre aucun argument.
Dans le corps de la fonction vous mettez les instructions destin&#233;es &#224; &#234;tre ex&#233;cut&#233;es.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de fonction&lt;/strong&gt;&lt;/p&gt; &lt;div style='text-align: left;' class='spip_code' dir='ltr'&gt;&lt;code&gt;function affiche_text_gras($texte){&lt;br /&gt; echo &quot;&lt;b&gt;&quot;.$texte.&quot;&lt;/b&gt;&quot;; &lt;br /&gt; }&lt;/code&gt;&lt;/div&gt; &lt;p&gt; // affiche un texte en gras&lt;/p&gt; &lt;p&gt;Une fonction peut &#233;galement renvoyer une valeur &#224; son ex&#233;cution gr&#226;ce au mot-cl&#233; : &lt;i&gt;return&lt;/i&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de fonction&lt;/strong&gt;&lt;/p&gt; &lt;div style='text-align: left;' class='spip_code' dir='ltr'&gt;&lt;code&gt;function carre($nb){&lt;br /&gt; return $nb*$nb; //retourne le carr&#233; d'un nombre&lt;br /&gt; }&lt;/code&gt;&lt;/div&gt;
&lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_1&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;L'appel de la fonction&lt;/h3&gt; &lt;p&gt;l'appel se fait de fa&#231;on explicite :&lt;/p&gt; &lt;ul class=&quot;spip&quot;&gt;&lt;li&gt; Si la fonction retourne une variable, il faut affecter &#224; une variable le r&#233;sultat de la fonction : $variable = NomDeLaFonction($arg1,$arg2, &#8230;.) ; // autant de $arg que d'argument&lt;/li&gt;&lt;li&gt; Si la fonction n'en retourne pas :
NomDeLaFonction($arg1,$arg2&#8230;) ; // autant de $arg que d'argument&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
		&lt;hr /&gt;
		&lt;div class='rss_notes'&gt;&lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;[&lt;a href='#nh1' id='nb1' class='spip_note' title='Notes 1' rev='footnote'&gt;1&lt;/a&gt;] &lt;span class='csfoo htmlb'&gt;&lt;/span&gt;doit se faire obligatoirement avant l'appel de la fonction&lt;/p&gt;&lt;/div&gt;
		
		</content:encoded>


		

	</item>
<item xml:lang="fr">
		<title>Fonction multi-r&#233;sultat</title>
		<link>http://www.6tech.net/Fonction-multi-resultat.html</link>
		<guid isPermaLink="true">http://www.6tech.net/Fonction-multi-resultat.html</guid>
		<dc:date>2008-04-08T21:07:41Z</dc:date>
		<dc:format>text/html</dc:format>
		<dc:language>fr</dc:language>
		<dc:creator>Yannick Domenjoud</dc:creator>


		<dc:subject>PHP</dc:subject>
		<dc:subject>Fonctions</dc:subject>

		<description>Il arrive souvent lors de construction de fonctions personnalis&#233;es en PHP de vouloir retourner plusieurs variables calcul&#233;es par la fonction en une fois plut&#244;t que de cr&#233;er une fonction diff&#233;rente par traitement. Retour par tableau Cette solution consiste en fait &#224; renvoyer un tableau contenant les diff&#233;rents r&#233;sultats. L'absence de typage en PHP nous permet facilement de renvoyer plusieurs r&#233;sultat de types diff&#233;rents dans une m&#234;me structure. Exemple de retour par tableau On veux pouvoir r&#233;cup&#233;rer (...)

-
&lt;a href="http://www.6tech.net/-Tutoriaux-.html" rel="directory"&gt;Tutoriaux&lt;/a&gt;

/ 
&lt;a href="http://www.6tech.net/+-PHP-+.html" rel="tag"&gt;PHP&lt;/a&gt;, 
&lt;a href="http://www.6tech.net/+-Fonctions-+.html" rel="tag"&gt;Fonctions&lt;/a&gt;

		</description>


 <content:encoded>&lt;div class='rss_texte'&gt;&lt;span class='csfoo somm'&gt;&lt;/span&gt;&lt;div class=&quot;cs_sommaire cs_sommaire_avec_fond&quot; id=&quot;outil_sommaire&quot;&gt; &lt;div class=&quot;cs_sommaire_inner&quot;&gt; &lt;div class=&quot;cs_sommaire_titre_avec_fond&quot;&gt; Sommaire &lt;/div&gt; &lt;ul&gt; &lt;li&gt;&lt;a title=&quot;Retour par tableau&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire_0&quot;&gt;Retour par tableau&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Le passage par r&#233;f&#233;rence&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire_1&quot;&gt;Le passage par r&#233;f&#233;rence&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;La modification par global&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire_2&quot;&gt;La modification par global&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt;&lt;span class='csfoo somm'&gt;&lt;/span&gt;&lt;p&gt;Il arrive souvent lors de construction de fonctions personnalis&#233;es en PHP de vouloir retourner plusieurs variables calcul&#233;es par la fonction en une fois plut&#244;t que de cr&#233;er une fonction diff&#233;rente par traitement.&lt;/p&gt; &lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_0&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;Retour par tableau&lt;/h3&gt; &lt;p&gt;Cette solution consiste en fait &#224; renvoyer un tableau contenant les diff&#233;rents r&#233;sultats. L'absence de typage en PHP nous permet facilement de renvoyer plusieurs r&#233;sultat de types diff&#233;rents dans une m&#234;me structure.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de retour par tableau&lt;/strong&gt;
&lt;i&gt;On veux pouvoir r&#233;cup&#233;rer les carr&#233;s et les cubes d'un nombre &#224; partir d'une seule fonction&lt;/i&gt;&lt;/p&gt; &lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;
&lt;span style=&quot;color: #0000BB&quot;&gt;&lt;?php&lt;br /&gt;unction calcul_puissance&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$n&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;){&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss1 &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$n&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss2 &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss1&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$n&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss3 &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss2&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$n&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$table &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= array(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss1&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss2&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$puiss3&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// cr&#233;er un tableau avec les valeurs $puiss1, $puiss2 et $puiss3 &lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$table&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// retourne le tableau pr&#233;c&#233;demment cr&#233;&#233;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;&lt;/p&gt; &lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_1&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;Le passage par r&#233;f&#233;rence&lt;/h3&gt; &lt;p&gt;Vous pouvez modifier directement des variables du script en les passant par r&#233;f&#233;rence &#224; la fonction avec le pr&#233;fix &lt;strong&gt;&amp;&lt;/strong&gt; sur le nom de variable. Toutes modifications effectu&#233;es intra-muraux dans la fonction affectera la variable pass&#233;e en param&#232;tre par r&#233;f&#233;rence &#224; la fonction.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de passage par r&#233;f&#233;rence&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;
&lt;span style=&quot;color: #0000BB&quot;&gt;&lt;?php&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;assign_valeur&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$var&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;){&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$value &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$var&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$chaine &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'Voici une chaine de test'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;assign_valeur&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&amp;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$chaine&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'2e chaine de test'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;);&lt;br /&gt;echo &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$chaine&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;La variable &lt;i&gt;$chaine&lt;/i&gt; contenant une valeur d'origine sera modifi&#233;e directement au niveau du script par la fonction &lt;strong&gt;assign_valeur&lt;/strong&gt; qui lui assignera la valeur contenu dans le deuxi&#232;me param&#232;tre de la fonction ( en l'occurence, &lt;i&gt;echo $chaine&lt;/i&gt; affichera &quot;2e chaine de test&quot; )&lt;/p&gt; &lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_2&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;La modification par &lt;strong&gt;global&lt;/strong&gt;&lt;/h3&gt; &lt;p&gt;Une autre solution un peu du m&#234;me genre que la pr&#233;c&#233;dente permet de modifier une variable du script &#224; partir de la fonction. Il faut pour cela sp&#233;cifier avant traitement de la variable par la fonction que la variable est &#224; modifier au niveau du script g&#233;n&#233;ral par le mot clef &lt;strong&gt;global&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de modification par global&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;
&lt;span style=&quot;color: #0000BB&quot;&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;&lt;br /&gt;function &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(){ &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// d&#233;claration de la fonction&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;global &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// it&#233;ration du nombre de pizza&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// ajoute une pizza&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// ajoute une pizza&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;echo &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;Nombre de pizza(s) : &quot;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;R&#233;sultat : &lt;strong&gt;Nombre de pizza(s) : 2 &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;On sp&#233;cifie au niveau de la fonction que la variable $nb_pizza &#224; utiliser n'est pas une nouvelle variable mais bien celle d&#233;finie pr&#233;c&#233;demment au d&#233;but du script. Si jamais vous enlevez le mot clef &lt;strong&gt;global&lt;/strong&gt;, la fonction essayera de cr&#233;er une nouvelle variable et l'incr&#233;mentera de un. Cependant &#224; la sortie de la fonction la variable du m&#234;me nom au niveau du script restera inchang&#233;e et gardera sa valeur initiale : 0&lt;/p&gt;&lt;/div&gt;
		
		</content:encoded>


		

	</item>
<item xml:lang="fr">
		<title>La surcharge de fonctions</title>
		<link>http://www.6tech.net/La-surcharge-de-fonctions.html</link>
		<guid isPermaLink="true">http://www.6tech.net/La-surcharge-de-fonctions.html</guid>
		<dc:date>2008-04-08T21:03:07Z</dc:date>
		<dc:format>text/html</dc:format>
		<dc:language>fr</dc:language>
		<dc:creator>Yannick Domenjoud</dc:creator>


		<dc:subject>PHP</dc:subject>
		<dc:subject>Fonctions</dc:subject>

		<description>&lt;p&gt;Comment palier au manque cruel de PHP sur la surcharge de fonction (inh&#233;rent directement de l'absence de typage de donn&#233;es )&lt;/p&gt;

-
&lt;a href="http://www.6tech.net/-Tutoriaux-.html" rel="directory"&gt;Tutoriaux&lt;/a&gt;

/ 
&lt;a href="http://www.6tech.net/+-PHP-+.html" rel="tag"&gt;PHP&lt;/a&gt;, 
&lt;a href="http://www.6tech.net/+-Fonctions-+.html" rel="tag"&gt;Fonctions&lt;/a&gt;

		</description>


 <content:encoded>&lt;div class='rss_texte'&gt;&lt;div class=&quot;cs_sommaire cs_sommaire_avec_fond&quot; id=&quot;outil_sommaire&quot;&gt; &lt;div class=&quot;cs_sommaire_inner&quot;&gt; &lt;div class=&quot;cs_sommaire_titre_avec_fond&quot;&gt; Sommaire &lt;/div&gt; &lt;ul&gt; &lt;li&gt;&lt;a title=&quot;Appel de fonction classique&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire_0&quot;&gt;Appel de fonction classique&lt;/a&gt;, p1&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Appel de m&#233;thode d&amp;#39;objet&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2#outil_sommaire_1&quot;&gt;Appel de m&#233;thode d'objet&lt;/a&gt;, p2&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Surchage de getter et setter&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2#outil_sommaire_2&quot;&gt;Surchage de getter et setter&lt;/a&gt;, p2&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Le gestionnaire d&amp;#39;erreur de PHP&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2#outil_sommaire_3&quot;&gt;Le gestionnaire d'erreur (&#8230;)&lt;/a&gt;, p2&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt;&lt;div id='decoupe_haut' class='pagination decoupe_haut'&gt;
&lt;img class=&quot;no_image_filtrer&quot; alt=&quot;Page pr&#233;c&#233;dente&quot; title=&quot;Page pr&#233;c&#233;dente&quot; src=&quot;http://www.6tech.net/plugins/auto/couteau_suisse/img/decoupe/precedent_off.gif&quot;/&gt; &lt;span class=&quot;cs_pagination_off&quot;&gt;1&lt;/span&gt; &lt;a title=&quot;Page 2 : Appel de m&#233;thode d&amp;#39;objet&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2&quot;&gt;2&lt;/a&gt; &lt;a href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2&quot;&gt;&lt;img class=&quot;no_image_filtrer&quot; alt=&quot;Page suivante&quot; title=&quot;Page suivante&quot; src=&quot;http://www.6tech.net/plugins/auto/couteau_suisse/img/decoupe/suivant.gif&quot;/&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;Qui n'a jamais voulu cr&#233;er deux fonctions diff&#233;rentes mais avec un profil relativement proche ( m&#234;me nom, mais le dernier param&#232;tre serait un entier suppl&#233;mentaire par exemple) ou alors a d&#233;fini maladroitement la m&#234;me fonction dans plusieurs fichiers mais d&#233;cide de modifier son profil ?&lt;/p&gt; &lt;p&gt;Il existe pour cela plusieurs m&#233;thodes qui d&#233;pendent majoritairement de la structure que vous employez.&lt;/p&gt; &lt;h3 class=&quot;spip&quot; id=&quot;outil_sommaire_0&quot;&gt;&lt;a title=&quot;Sommaire&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23#outil_sommaire&quot; class=&quot;sommaire_ancre&quot;&gt; &lt;/a&gt;Appel de fonction classique&lt;/h3&gt; &lt;p&gt;Cette solution consiste &#224; cr&#233;er une fonction sans param&#232;tres ( ou avec uniquement les arguments communs &#224; la fonction de base et ses &#233;ventuelles surcharges ) et d'appliquer un traitement diff&#233;rent en fonction du nombre d'arguments pass&#233;s en param&#232;tres gr&#226;ce aux fonctions : &lt;strong&gt;func_get_args()&lt;/strong&gt; et &lt;strong&gt;func_num_args()&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de fonction &#224; nombre d'argument variable&lt;/strong&gt;
On veut r&#233;cup&#233;rer le plus grand nombre parmi les param&#232;tres fournis &#224; la fonction si leur nombre est pair, ou le plus petit si leur nombre est impair.&lt;/p&gt; &lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;
&lt;span style=&quot;color: #0000BB&quot;&gt;&lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;minMaxFromValue&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(){&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$argsCount &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;func_num_args&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;();&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$argsList &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;func_get_args&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;();&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$selected &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$argsList&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;];&lt;br /&gt; for(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$argsCount&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;++)&lt;br /&gt; if( &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$argsCount&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;0 &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;? &lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$args&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;] &gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$selected &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;:&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$args&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;] &lt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$selected &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;)&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$selected &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$args&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;];&lt;br /&gt; return &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$selected&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;On peut donc influencer le comportement d'une fonction &#224; travers son nombre d'argument.&lt;/p&gt; &lt;p&gt;Une autre m&#233;thode plus simple consiste &#224; positionner des arguments &#224; des valeurs par d&#233;faut dans le cas o&#249; ils ne sont pas sp&#233;cifi&#233;s.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Exemple de fonction &#224; argument par d&#233;faut&lt;/strong&gt;
Un exemple sur les pizzas.&lt;/p&gt; &lt;p&gt;&lt;span class='csfoo htmla'&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;
&lt;span style=&quot;color: #0000BB&quot;&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt;&lt;br /&gt;function &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$action &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;){&lt;br /&gt; &lt;br /&gt; global &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; &lt;br /&gt; switch(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$action&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;){&lt;br /&gt; &lt;br /&gt; case &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'affichage'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;:&lt;br /&gt; echo &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'Nombre de pizza actuel : '&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'&lt;br&gt;'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; break;&lt;br /&gt; &lt;br /&gt; case &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'envoyer'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;:&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; echo &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'Il reste encore '&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;' pizza(s)&lt;br&gt;'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; break;&lt;br /&gt; &lt;br /&gt; case &lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;:&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb_pizza &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$nb&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;;&lt;br /&gt; break;&lt;br /&gt; &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// ajoute 3 pizza&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'affichage'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// affiche les pizzas&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// ajoute 1 pizza ( valeur par d&#233;faut)&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'affichage'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// affiche les pizzas&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;ajouter_pizza&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;'envoyer'&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #FF8000&quot;&gt;// Enl&#232;ve 2 pizzas et affiche le nombre restant&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/code&gt;&lt;span class='csfoo htmlb'&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id='decoupe_bas' class='pagination decoupe_bas'&gt;
&lt;img class=&quot;no_image_filtrer&quot; alt=&quot;Page pr&#233;c&#233;dente&quot; title=&quot;Page pr&#233;c&#233;dente&quot; src=&quot;http://www.6tech.net/plugins/auto/couteau_suisse/img/decoupe/precedent_off.gif&quot;/&gt; &lt;span class=&quot;cs_pagination_off&quot;&gt;1&lt;/span&gt; &lt;a title=&quot;Page 2 : Appel de m&#233;thode d&amp;#39;objet&quot; href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2&quot;&gt;2&lt;/a&gt; &lt;a href=&quot;http://www.6tech.net/spip.php?page=backend&amp;id_mot%5B%5D=1&amp;id_mot%5B%5D=23&amp;artpage=2-2&quot;&gt;&lt;img class=&quot;no_image_filtrer&quot; alt=&quot;Page suivante&quot; title=&quot;Page suivante&quot; src=&quot;http://www.6tech.net/plugins/auto/couteau_suisse/img/decoupe/suivant.gif&quot;/&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
		
		</content:encoded>


		

	</item>



</channel>

</rss>
