<?php class Index { public function getBody() { echo $this->getMenu(); } private function getMenu() { $menu = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/menu.php'); return $this->replaceStringMenu($menu); } private function replaceStringMenu($menu) { $main = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/main.php'); return str_replace("%menu%", $menu, $main); } } ?> 

I call on the main getBody() , and the code from menu.php processed as html , what can I do to make it work like php ?

Update

With include, it processes the code, only I change the expression of the menu block, and the result is displayed just at the top of the page.

 <?php require_once '/index.class.php'; $obj = new Index; $obj->getBody(); ?> 
  • do he include? - BOPOH
  • <? php echo getBody ()?> - Naumov
  • How do you call show? - Naumov

2 answers 2

You need to make an eval($obj->getBody); if you want to execute code from a file. Documentation , of course, read in advance.

But maybe you still have to store the menu, for example, in a .ini file?

  • Changed the format to .ini, and used eval . the same result - O.Vykhor
  • You still need to replace echo $this->getMenu(); in getBody echo $this->getMenu(); on return $this->getMenu(); . But return the extension, please back. Ini-files are quietly delivered to the default web server, no need for everyone to see them. I meant that you would build the menu from the configuration file, and not do a text replacement in the executable file. So it is close to php-injection ... - Andrewus
  • what is the difference in getBody() return or echo , then getBody() should be output to the main one, but it works the same way. And how to build a menu from the config file? - O.Vykhor
  • The difference is fundamental: return the text further (and correctly pass to eval ) or output the test to the standard stream . It is easy to build a menu: you read the config into an array, for example, and by some rules output certain menu items. - Andrewus
  • eval swears at "<" but I insert the php code in html and then send html along with php to eval , no brackets in any way - O.Vykhor

Finally fixed

 <?php class Index { public function getBody() { return $this->getMenu(); } private function getMenu() { ob_start(); include($_SERVER['DOCUMENT_ROOT'] . '/menu.php'); $menu = ob_get_contents(); ob_end_clean(); return $this->replaceString("%menu%", $menu); } private function replaceString($pattern, $menu) { $main = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/main.php'); return str_replace($pattern, $menu, $main); } } ?>