Is it possible in php to bring custom classes to standard variable types? That is, so that you can do something like this:

<? class myClass{ var $str; function myClass($str) { $this->str = $str; } function boolean(){ return strlen($this->str); } function string(){ return $this->str; } }; $mc = new myClass('string'); if($mc) echo "String '$mc' is not empty."; else echo "String is empty." ?> 

I was looking for an answer in nete, but apparently I was looking badly.

    1 answer 1

    Yes. You are absolutely right!

    but apparently looking bad.

    It is necessary to declare the "magic" method. __toString.

     ... function __toString(){ return $this->str; } ... $mc = new myClass('string'); echo "String '$mc' is not empty."; 

    Unfortunately, this only works for strings.

    • Thank. Apparently, to cast to other types, you will have to directly call class methods ... - ling