Question

I'm making a simple MVC framework with PHP. Now i have to decide whether i should use:
(Among this methods)

ob_start();
include(....);
ob_get_contents();

Or

include(....);

Seriously which one is the standard (or what should be?) .. since i can see all my $variables (assigned all the way long above) are RE-CALLABLE from the Template files .. being called by either method above.

Was it helpful?

Solution

From your question I can assume that you don't use any template engine and your views contain real PHP variables but not something like %var%. If your framework is really simple and you only have a single template per page/controller call, the output buffering is not required. Like you said, you simply put your single include after you have all the variables assigned. Otherwise, if you have sub-templates or things like that (so you have more than one include), output buffering should be used. A key feature here is a possibility to store ob_get_contents() result in a variable, so you could work with views content as with simple strings. This allows considering of some other nice features like caching of views content, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top