toString Method

 <?php

//__toString() is a Magic Method.
//When we try to print any Class's Object as a String at that time __toString() method will
//automatically Call.
//__toString() function will automatically call when we try to print any Object(of Class) as a String.

class Abc
{
    public function __toString()
    {
        return "Can't print Object as a String of Class : " . get_class($this);
    }
}
$test = new Abc();
echo $test;

//  OUTPUT:
//Can't print Object as a String of Class : Abc

?>

Comments

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database