Portál AbcLinuxu, 13. července 2025 13:31
class View{ private $model; public function __construct($model){ $this->model = $model; } private function title(){ return $this->model->title(); } public function __toString(){ return '<!DOCTYPE HTML>'. '<html>'. '<head>'. '<title'.$this->title().'</title>'. '</head>'. '<body>'. '</body>'. '</html>'; } } class Model{ public function __construct(){ } public function title(){ return 'title'; } } class Controller{ private $html; public function __construct(){ $model = new Model (); $this->html = $view = new View ($model); } public function __toString(){ return $this->html; } } $controller = new Controller(); echo $controller;
public function __toString() { return '' . $this->html; }
Ale fuj :D.
http://php.net/manual/en/function.strval.php
Normalne to pretypuj:
public function __toString(){ return (string)$this->html; }
Tak jde o to ze pokud mate nejnovejsi php, tak muzete (a mel by ste) urcit navratovy typ funkce
... class Controller{ private $html; public function __construct(){ $model = new Model (); $this->html = $view = new View ($model); } public function __toString():string { return $this->html; } } ...Potom uz neni treba pretypovat
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.