Magic method toString PHP

360 views Asked by At

If i create two classes in one php file, and define the magic method __toString() for both,which one of them will be executed?Because i have already done this and one of them is executed...i just don't know why?

public function __toString(){
    return $this->pla_id." Nom = ".$this->pla_nom.", Rayon = ".$this->pla_rayon.", Gravitation = ".$this->pla_gravitation;
}

public function __toString(){
        return $this->sat_id."  ".$this->sat_nom."  ".$this->sat_rayon." ".$this->sat_rotation;
    }

and in another file:

$mars = new Planete(); $mars->setNom("Mars"); $mars->setRayon(3397); 
$mars->setGravitation(3.69);
$ph = new Satellite(); $ph->setNom("Phobos"); 
$ph->setPlanete($mars->getNom()); $ph->setRotation(0.32);
echo $mars;

but only the first one appears!

1

There are 1 answers

0
Qossai Labyad On

I forgot to echo the other class instance, my fault! the answer is to add echo $ph in the last code sorry!!