POO Appel de methodes en cascade
Bonjour,
J'ai un objet pour lequel je voudrai appeler une de ses méthode en cascade. Pour des raisons de facilité je voudrai déclarer et utiliser des variables a la volée comme en Java, mais je suis confronté au problème suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class String{
private $chaine;
public function __construct(){
if (func_num_args() > 0){$this->chaine = func_get_args()[0];}
else $this->chaine = null;
}
public function get(){
return $this->chaine;
}
public function concat(String $str){
$this->chaine .= $str->get();
}
public function afficher(){
echo($this->chaine);
}
} |
Code:
1 2 3 4 5 6 7 8 9
|
//cas ok
$titi = new String("titi ");
$toto = new String("toto ");
$toto->concat($titi);
$toto->concat($titi);
$toto->afficher();
-> toto titi titi |
Code:
1 2 3 4 5 6
|
//cas ko
$toto->concat($titi->concat($titi));
-> Argument 1 passed to String::concat() must be an instance of String, null given, called in |
Code:
1 2 3 4 5
| //cas ko
$toto->concat($titi)->concat($titi);
-> Call to a member function concat() on a non-object in ... |
Code:
1 2 3 4 5 6 7
|
//cas ko
$titi = new String("titi ");
$toto = (new String("toto "))->concat($titi);
$toto->afficher();
-> Call to a member function afficher() on a non-object |
Auriez vous svp une idee de la cause de mon problème.
Merci d'avance