[RESOLU] Convertir un objet en string

Mammouth du PHP | 725 Messages

10 mars 2013, 17:57

Bonjour,

J'ai 2 fonctions checkDay() et checkMonth(), je devais recuperer une variable de checkDay et l'implementer a checkMonth, mais cette variable devenait un objet, comment la convertir en string?

	// check day
    public function checkDay() {
        if(empty($this->day) OR ($this->day > 31))
        {
            $day_check = 'DayNotValid';
        }
        elseif(($this->day > 29) && ($this->month == 02))
        {
            $day_check = 'Daybad29';
        }
        else
        {
            $day_check = 'DayValid';
        }
       
        $this->day_check = $day_check;
	return $this->day_check;
    } // end function checkDay($this->day)


	// check month
    public function checkMonth() {

        echo $this->day; // devient un objet et non une string

    if(($this->day > 30) && (!in_array($this->month, $this->mw31d)))
        {
            $month_check = 'Monthbad31';
        }
        else
        {
            $month_check = 'MonthValid';
        }
           
        $this->month_check = $month_check;
       
        return $this->month_check;
    } // end function checkMonth($this->month)
 
merci a vous

ViPHP
xTG
ViPHP | 7331 Messages

11 mars 2013, 10:28

Pourtant cet objet est capable d'être comparé à des entiers d'après ta fonction précédente donc je pense pas qu'il y ait de problème à l'utiliser directement.
Si ce n'est pas le cas beh va falloir nous donner le code de la class de cet objet...
Et sinon tu peux vérifier que cette class ne comporte pas une fonction __toString().

Mammouth du PHP | 725 Messages

12 mars 2013, 01:26

voici le code de la class:
<?php

class DOBD {

    // params
    private $errors = array(), $day, $month, $year;

    // get user group, level and id
    public function __construct($day, $month, $year) {
        
        // set months with 31 days
        $mw31d = array(1, 3, 5, 7, 8, 10, 12);
        
        $this->day      = $day;
        $this->month    = $month;
        $this->year     = $year;
        $this->mw31d    = $mw31d;
    } // end function __construct
    
    
    // check the day    
    public function checkDay() {
        // check if Feb contains more than 29 days
        if(empty($this->day) OR ($this->day > 31))
        {
            $day_check = 'DayNotValid';
        }
        elseif(($this->day > 29) && ($this->month == 02))
        {
            $day_check = 'Daybad29';
        }
        else
        {
            $day_check = 'DayValid';
        }
        
        $this->day_check = $day_check;
        //echo 'check day '.$this->day_check.'<br />';
        
        return $this->day_check;
    } // end function checkDay($this->day)
    

    public function __toString()
    {
        return $this->day;
    }

    
    // check the month
    public function checkMonth() {
        
        var_dump($this->day);
        
        if(empty($this->month) OR ($this->month > 12))
        {
            $month_check = 'MonthNotValid';
        }
        // check if the selected month has 31 days
        elseif(($this->day > 30) && (!in_array($this->month, $this->mw31d)))
        {
            $month_check = 'Monthbad31';
        }
        else
        {
            $month_check = 'MonthValid';
        }
            
        $this->month_check = $month_check;
        
        return $this->month_check;
        //echo 'check month '.$this->month_check.'<br />';
    } // end function checkMonth($this->month)
    
    
    // check Leap year
    public function isLeap()
    {
        if(((($this->year % 4) == 0) AND ($this->year % 100 != 0)) OR ($this->year % 400 == 0))
        {
            return TRUE;
        }
            return FALSE;        
    }
    
    // check the year
    public function checkYear() {
    if(empty($this->year))
    {
        $year_check = 'YearNotValid';
    }
    // check if the year is leap
    elseif((!$this->isLeap($this->year) && ($this->day >= 29) && ($this->month == 2)))
    {
        $year_check = 'yearLeap';
    }
    else
    {
    	$year_check = 'YearValid';
    }
        
        $this->year_check = $year_check;
        return $this->year_check;
        //echo 'check year '.$this->year_check.'<br />';
    
    } // end function checkYear($this->year) {
    
    
    // check if the DOB is greater than today date (NOT YET BORNED)
    public function checkDOBD()
    {
        $today = date('d-m-Y');
        $todayTimeStamp = strtotime($today);
        //echo $todayTimeStamp.'<br >';
        
        $dob = $this->day.'-'.$this->month.'-'.$this->year;
        $dobTimeStamp = strtotime($dob).'<br />';
        //echo $dobTimeStamp.'<br >';
    
        if($todayTimeStamp <= $dobTimeStamp)
        {
            $dob_check = 'NotYetBorn';
            //return false;
        }
        elseif(($this->year_check != 'YearValid') OR ($this->month_check != 'MonthValid') OR ($this->day_check != 'DayValid'))
        {
            $dob_check = 'DOBNotValid';
            //return false;
        }
        else
        {
            $dob_check = 'DOBValid';
            //return true;
        }
        
        $this->dob_check = $dob_check;
        //echo 'so dobd is ===> '.$this->dob_check.'<br />';
        return $this->dob_check;
    } // end function checkDOBD()
    
} // end class DOBD

?>

ViPHP
xTG
ViPHP | 7331 Messages

12 mars 2013, 10:10

C'est la class Day qui nous intéresse, pas celle là.

Mammouth du PHP | 725 Messages

12 mars 2013, 16:36

C'est la class Day qui nous intéresse, pas celle là.
Je n'ai qu'une seule class qui contient des fonctions

ViPHP
xTG
ViPHP | 7331 Messages

12 mars 2013, 16:39

J'avais lu un peu en diagonale...
C'est donc $day_check qui te pose problème ?
Tu la déclares explicitement en String donc je vois pas pourquoi tu récupérerai un objet.
Que te donnes un gettype($day_check) ?

Mammouth du PHP | 571 Messages

12 mars 2013, 18:24

comme indiqué dans la classe DOBD, $this->day n'est pas un objet mais un attribut de classe. L'attribut $this->day n'est pas typé donc tout transtypage(conversion) vers un string serait un non sens.

Personnellement je ne vois aucun problème dans ton code sauf si je ne saisi pas bien ce que tu veux faire.

J'ai 2 fonctions checkDay() et checkMonth(), je devais recuperer une variable de checkDay et l'implementer a checkMonth, mais cette variable devenait un objet, comment la convertir en string?
Laquelle variable veux-tu récupérer dans dans checDay(je ne la vois pas)?
si tu veux afficher $this->day il est mieux de le vérifier avant de l'afficher:
        // check month
    public function checkMonth() {

    if(!empty($this->day)){
        echo $this->day; // devient un objet et non une string
      }

    if(($this->day > 30) && (!in_array($this->month, $this->mw31d)))
        {
            $month_check = 'Monthbad31';
        }
        else
        {
            $month_check = 'MonthValid';
        }
           
        $this->month_check = $month_check;
       
        return $this->month_check;
    } // 
est-ce que ton problème ne se situe pendant l'appel de tes méthodes après avoir instancier ta classe?

Mammouth du PHP | 725 Messages

13 mars 2013, 02:55

@xTG: c'est $this->day qui me pose un probleme, gettype($this->day) me retourne string dans la fonction checkDay() et objet dans la fonction checkMonth().

public function checkDay(){
        echo gettype($this->day); // string
        }
    
public function checkMonth() {          
        echo gettype($this->day); // objet
}
Et un var_dump($this->day) me retourne ca:

Code : Tout sélectionner

object(DOBD)#3 (8) { ["errors":"DOBD":private]=> array(0) { } ["fname":"DOBD":private]=> NULL ["farray":"DOBD":private]=> NULL ["day":"DOBD":private]=> string(2) "31" ["month":"DOBD":private]=> string(1) "4" ["year":"DOBD":private]=> string(1) "0" ["mw31d"]=> array(7) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(7) [4]=> int(8) [5]=> int(10) [6]=> int(12) } ["day_check"]=> string(8) "DayValid" }

@yann18:

Code : Tout sélectionner

PHP Catchable fatal error: Object of class DOBD could not be converted to string in class_checkDOBD.php on line 64
la ligne 64 est: echo $this->day; // devient un objet et non une string

ViPHP
xTG
ViPHP | 7331 Messages

13 mars 2013, 10:16

C'est un petit peu, beaucoup, à la folie, pas normal !
Si tu as ce changement c'est qu'entre deux tu réaffectes une valeur à $this->day en lui passant un objet DOBD.
Vérifies dans ton code toutes les affectations à cette variable, et places des messages de debug d'emplacement et de gettype pour trouver l'affectation qui te fout le bouzin.

Mammouth du PHP | 725 Messages

13 mars 2013, 16:56

le code marche bien en local, par contre sur le serveur non

EDIT: j'ai teste la class seule sur le serveur ca marche, mais quand j'implique le formulaire (traitement via formulaire), ca me retourne objet

ViPHP
xTG
ViPHP | 7331 Messages

13 mars 2013, 17:29

Vérifies l'état de ton fichier serveur et local.
Ce n'est pas possible d'avoir deux traitements radicalement différents comme ça pour le même traitement et la même source.
C'est pas la configuration du serveur qui peut te changer ça, c'est une différence de code ou de traitement (et donc de code pour rediriger vers le mauvais traitement).

Mammouth du PHP | 725 Messages

14 mars 2013, 01:34

bonjour xTG, je vais vous dire c'etait quoi l'erreur, disant l'erreur banale, mais ne me ban pas du forum :)

Comme j'ai pre-cite, l'utilisation de la class seule ne genere aucune erreur, mais quand je fais un traitement formulaire, la ou il y a le probleme, j'ai lu le fichier de traitement du formulaire, j'ai trouve un new DOBD qui a ete declaree en double :'(