简体   繁体   中英

How to display a class from another class in PHP

How to display a class from another class in PHP ?

 class Layout {
      public $var;

      public function __construct() {
           $this->var = 'test';
      }

      public function __toString() {
           return $this->var;
      }
 }

 class Template {
      private $var_layout;

      public function __construct() {
           $obj = new Layout;
           $this->var_layout = $obj;
      }

      public function __toString() {
           return $this->var_layout;
      }
 }

 $template = new Template();

 echo($template);

Error message: Method Template::__toString() must return a string value

Please help, thank you very much..

return $this->var_layout; in the Template class does not return a string, it returns an object. Make it return a string by calling the __toString() method of that object explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM