Object of class -> string
Bonjour,
J'ai fait une class pour apprendre un peu le contrôle return.
Voici ma class (class.php) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php
class testClass
{
public function add($arg) {
$arg = $arg + 1;
//return ; //meme erreur
//return $arg;//meme erreur
return (string)$arg;//meme erreur
}
} |
et voici mon fichier test.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| include_once ('class.php');
$obj = new testClass();
$nombre = 2;
echo '<p>' .$nombre . '</p>';
$obj->add($nombre);
$total = $obj;
//var_dump($total); // object(testClass)[1]
//print_r($total); // testClass Object ( )
echo $total; //meme erreur
echo (string)$total; //meme erreur |
alors j'ai l'erreur fatale :
Citation:
Catchable fatal error: Object of class testClass could not be converted to string in C:\biblio\class.Objet\test.php on line 20
Alors comment je peux trouver la valeur de ma variable $total ?
Merci