IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Fonction récursive avec un paramètre en objet


Sujet :

Langage PHP

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut Fonction récursive avec un paramètre en objet
    Bonjour à tous !

    Alors voilà, pour l'entreprise pour laquelle je travaille, j'ai créé un WebService.
    J'ai des objets avec des propriétés en string qui passent dans le WSDL. Mais il y a une curieuse chose ! certains textes passent mal, ceux disposant de \r\n, seul le \n arrive à destination, le \r est complètement oublié O_o.

    J'ai donc créé une fonction récursive permettant de nettoyer un objet.

    voici mon exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    <?php
     
    class myCLass
    {
        public $boolean = false;
        public $integer = 0;
        public $double = 0.0;
        public $string = null;
     
        function myCLass($boolean, $integer, $double, $string)
        {
            $this->boolean = $boolean;
            $this->integer = $integer;
            $this->double = $double;
            $this->string = $string;
        }
    }
     
    function FixObjectPDA($myParam)
    {
        switch(gettype($myParam))
        {
            case 'array' :
                foreach($myParam AS $oneObject)
                {
                    $myParam[array_search($oneObject, $myParam)] = FixObjectPDA($oneObject);
                }
            break;
     
            case 'object' :
                foreach($myParam AS $myField)
                {
                    $myParam[array_search($myField, $myParam)] = FixObjectPDA($myField);
                }
            break;
     
            case 'string' :
                return FixStringPDA($myParam);
            break;
     
            default :
                return  $myParam;
            break;
        }
    }
     
    function FixStringPDA($str)
    {
        $FindLineFeed = preg_match("#\n#", $str);
        $FindCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && !$FindCarriage)
        {
            return preg_replace("/(\n)/", "\r\n", $str);
        }
        return $str;
    }
     
    $myObject[] = new myCLass(true, 3, 1.8, "essai\r\n avec des retour chariot d'après ce qu'il semble !");
    $myObject = FixObjectPDA($myObject);
    var_dump($myObject);
     
    ?>
    Seulement voilà, je ne suis pas un grand expert en fonction récursive.

    Le fonctionnement :
    Je peux passer en paramètre de la fonction un Objet ou une collection d'Objets.
    Pour chaque éléments de la collection, on relance la fonction.
    Dans le cas d'un champ string, on répare les données.
    Dans le cas ou il s'agit d'un autre type de données, on ne fait aucun traitement.

    ATTENTION ! Un objet peut posséder des sous-objets !

    Le problème est que je n'arrive pas à obtenir de retour :/

    Pourriez-vous m'aider svp ? Merci.

  2. #2
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    je viens de modifier un peu mon code. Je pense pouvoir m'en sortir, mais j'ai un problème à ma ligne n°35 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    <?php
     
    class myCLass
    {
        public $boolean = false;
        public $integer = 0;
        public $double = 0.0;
        public $string = null;
     
        function myCLass($boolean, $integer, $double, $string)
        {
            $this->boolean = $boolean;
            $this->integer = $integer;
            $this->double = $double;
            $this->string = $string;
        }
    }
     
    function FixObjectPDA($myParam)
    {
        switch(gettype($myParam))
        {
            case 'array' :
                $myTab = array();
                foreach($myParam AS $oneObject)
                {
                    $myTab[] = FixObjectPDA($oneObject);
                }
                return $myTab;
            break;
     
            case 'object' :
                foreach($myParam AS $myField)
                {
                    $myParam[array_search($myField, $myParam)] = FixObjectPDA($myField);
                }
                return $myParam;
            break;
     
            case 'string' :
                return FixStringPDA($myParam);
            break;
     
            default :
                return  $myParam;
            break;
        }
    }
     
    function FixStringPDA($str)
    {
        $FindLineFeed = preg_match("#\n#", $str);
        $FindCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && !$FindCarriage)
        {
            return preg_replace("/(\n)/", "\r\n", $str);
        }
        return $str;
    }
     
    $myObject[] = new myCLass(true, 3, 1.8, "essai\r\n avec des retour chariot d'après ce qu'il semble !");
    $myObject = FixObjectPDA($myObject);
    var_dump($myObject);
     
    ?>
    auriez-vous une idée svp ?

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    j'ai réussi, et j'ai lutté !! -_-

    Voici donc tout mon travail :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    <?php
     
    class myCLass
    {
        public $boolean = false;
        public $integer = 0;
        public $double = 0.0;
        public $string = null;
     
        function myCLass($boolean, $integer, $double, $string)
        {
            $this->boolean = $boolean;
            $this->integer = $integer;
            $this->double = $double;
            $this->string = $string;
        }
    }
     
    function FixObject($myParam)
    {
        switch(gettype($myParam))
        {
            case 'array' :
                $myTab = array();
                foreach($myParam AS $oneObject)
                {
                    $myTab[] = FixObject($oneObject);
                }
                return $myTab;
            break;
     
            case 'object' :
                $myParam = ((array) ($myParam));
                foreach($myParam AS $myField)
                {
                    $myParam[array_search($myField, $myParam)] = FixObject($myField);
                }
                $myParam = ((object) ($myParam));
                return $myParam;
            break;
     
            case 'string' :
                return FixString($myParam);
            break;
     
            default :
                return $myParam;
            break;
        }
    }
     
    function FixString($str)
    {
        $FindLineFeed = preg_match("#\n#", $str);
        $FindCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && !$FindCarriage)
        {
            $str = preg_replace("/(\n)/", "\r\n", $str);
        }
     
        $FindLineFeed = preg_match("#\n#", $str);
        $FindDoubleCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && $FindDoubleCarriage)
        {
            $str = preg_replace("/(\r\r\n)/", "\r\n", $str);
        }
     
        return $str;
    }
     
    $myObject = new myCLass(true, 3, 1.8, "essai\r\n avec des retours chariot d'après ce qu'il semble !");
    $myObject = FixObject($myObject);
     
    echo $myObject->string;
     
    ?>

  4. #4
    Expert éminent
    Avatar de kdmbella
    Homme Profil pro
    Développeur Web
    Inscrit en
    Août 2010
    Messages
    799
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Août 2010
    Messages : 799
    Points : 7 039
    Points
    7 039
    Par défaut
    je pense que tu utilise mal ton foreach peut tu nous poster le message d'erreur ? retourner a cette ligne ?
    "L'humanité se divise en trois catégories : ceux qui ne peuvent pas bouger, ceux qui peuvent bouger, et ceux qui bougent."
    - Benjamin Franklin

    De l'aide en Javascript , consultez la FAQ JS.

    De l'aide sur le FrameWork JS DHTMLX : posez vos questions sur le forum des Bibliothèques & Frameworks JS.

  5. #5
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    j'ai réussi mon opération. voilà mon code final :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    <?php
     
    class myCLass
    {
        public $boolean = false;
        public $integer = 0;
        public $double = 0.0;
        public $string = null;
     
        function myCLass($boolean, $integer, $double, $string)
        {
            $this->boolean = $boolean;
            $this->integer = $integer;
            $this->double = $double;
            $this->string = $string;
        }
    }
     
    function FixObject($myParam)
    {
        switch(gettype($myParam))
        {
            case 'array' :
                $myTab = array();
                foreach($myParam AS $oneObject)
                {
                    $myTab[] = FixObject($oneObject);
                }
                return $myTab;
            break;
     
            case 'object' :
                $myParam = ((array) ($myParam));
                foreach($myParam AS $myField)
                {
                    $myParam[array_search($myField, $myParam)] = FixObject($myField);
                }
                $myParam = ((object) ($myParam));
                return $myParam;
            break;
     
            case 'string' :
                return FixString($myParam);
            break;
     
            default :
                return $myParam;
            break;
        }
    }
     
    function FixString($str)
    {
        $FindLineFeed = preg_match("#\n#", $str);
        $FindCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && !$FindCarriage)
        {
            $str = preg_replace("/(\n)/", "\r\n", $str);
        }
     
        $FindLineFeed = preg_match("#\n#", $str);
        $FindDoubleCarriage = preg_match("#\r#", $str);
        if($FindLineFeed && $FindDoubleCarriage)
        {
            $str = preg_replace("/(\r\r\n)/", "\r\n", $str);
        }
     
        return $str;
    }
     
    $myObject = new myCLass(true, 3, 1.8, "essai\r\n avec des retours chariot d'après ce qu'il semble !");
    $myObject = FixObject($myObject);
     
    echo $myObject->string;
     
    ?>
    merci quand même

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fonction récursive avec objet en argument
    Par mkrzemin dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 11/11/2010, 18h18
  2. Procedure stockee avec en paramètre un objet
    Par mellya dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 24/11/2007, 19h03
  3. Fonction 'failed' avec comme paramètre un HResult
    Par Leobaillard dans le forum Delphi
    Réponses: 1
    Dernier message: 08/08/2006, 20h29
  4. Réponses: 5
    Dernier message: 28/04/2006, 14h40
  5. Problème de fonction récursive avec un TcxDBTreeList
    Par isachat666 dans le forum Composants VCL
    Réponses: 1
    Dernier message: 05/12/2005, 13h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo