Defining a class's stringification : __toString « Class « PHP






Defining a class's stringification

 
class Person {
    protected $name;
    protected $email;
    
    public function setName($name) {
        $this->name = $name;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function __toString() {
        return "$this->name <$this->email>";
    }
}
  
  








Related examples in the same category

1.__toString( ) set a string value for the object that will be used if the object is ever used as a string.
2.Using the __toString() Method