Construct function

Fermé
Sofia419 Messages postés 8 Date d'inscription lundi 7 septembre 2015 Statut Membre Dernière intervention 28 novembre 2017 - 7 sept. 2015 à 12:49
 sofia419 - 8 sept. 2015 à 22:54
j'arrive pa a comprendre pourquoi on utilise le func_get_arg() dans la fonction de constructeur
<?php
class produit{
public $id_prod;
public $nom_prod;
public $photo_prod;
public $prix;
public $descriptions;
public $quantite;
public $id_categorie;



function __construct()
{
$num=func_num_args();
switch($num)
{
case 1:
//un seul paphotoramètre passé
$this->id_prod=func_get_arg(0);
break;
case 2:
//deux paramètres passés
$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
break;



case 3:

$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
$this->photo_prod=func_get_arg(2);

break;


case 4 :
$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
$this->photo_prod=func_get_arg(2);
$this->prix=func_get_arg(3);

break;

case 5:

$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
$this->photo_prod=func_get_arg(2);
$this->prix=func_get_arg(3);
$this->descriptions=func_get_arg(4);

break;


case 6:

$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
$this->photo_prod=func_get_arg(2);
$this->prix=func_get_arg(3);
$this->descriptions=func_get_arg(4);
$this->quantite=func_get_arg(5);

break;


case 7:

$this->id_prod=func_get_arg(0);
$this->nom_prod=func_get_arg(1);
$this->photo_prod=func_get_arg(2);
$this->prix=func_get_arg(3);
$this->descriptions=func_get_arg(4);
$this->quantite=func_get_arg(5);
$this->id_categorie=func_get_arg(6);

break;

default:
}

}

1 réponse

Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 526
8 sept. 2015 à 11:09
Salut,

Un petit coup d'oeil à la doc :
https://www.php.net/manual/fr/function.func-get-arg.php
https://www.php.net/manual/fr/function.func-num-args.php

func_get_arg() peut être utilisé conjointement à func_get_args() pour permettre aux fonctions utilisateurs d'accepter un nombre variable d'arguments.

Ainsi ta méthode __construct() peut être appelé avec 0, 1 ... ou 7 paramètres.

Bonne journée
0
merci infiniment ^_^
0