https://www.php.net/manual/en/function.var-export.php

class Printable1{
    public $testone;
    public $testtwo;
    public function __toString(){
        return(var_export($this,TRUE));
    }
}
$a = new Printable1;
$a->testone = 5;
$a->testtwo = 'foo';
var_dump($a);

the function __toString() it doesn't return anything I found some examples in the php documentation but I don't know how to use this function.
the var_export() function prints out all the attribute values in the class

   public function __toString(){
        return(var_export($this,TRUE));
    }

this code I found in php documention examples

 $a = new Printable1;
$a->testone = 5;
$a->testtwo = 'foo';
var_dump($a);

    <?php

    class Printable{
        function __toString() :string {
         return 'String';
        }
    }
    $p = new Printable;
    echo $p;
    echo "<br>";
    class Printable1{
        public $testone;
        public $testtwo;
        public function __toString(){
            return(var_export($this,TRUE));
        }
    }
    $a = new Printable1;
    //$a->testone = 5;
    //$a->testtwo = 'foo';
    //var_dump($a);
    echo $a;

    I think the function __toStringg is working well I am new with oop and functions

      Write a Reply...