Mega Code Archive

 
Categories / Php / Class
 

Using the __toString() Method

<?php     class User {             private $username;             function __construct($name) {                     $this->username = $name;             }             public function getUserName() {                     return $this->username;             }             function __toString() {                     return $this->getUserName();             }     }     $user = new User("john");     echo $user; ?>